Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
| 2 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 3 | #define _GNU_SOURCE |
| 4 | |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 5 | #include "config.h" |
Andrew Jeffery | 5ab7825 | 2022-02-17 21:04:59 +1030 | [diff] [blame] | 6 | |
| 7 | #define SD_LISTEN_FDS_START 3 |
| 8 | |
| 9 | #include "compiler.h" |
| 10 | #include "libmctp.h" |
| 11 | #include "libmctp-serial.h" |
| 12 | #include "libmctp-astlpc.h" |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 13 | #include "utils/mctp-capture.h" |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 14 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 15 | #include <assert.h> |
| 16 | #include <err.h> |
Andrew Jeffery | 6896d41 | 2020-03-11 09:25:32 +1030 | [diff] [blame] | 17 | #include <errno.h> |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 18 | #include <getopt.h> |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 19 | #include <limits.h> |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 20 | #include <poll.h> |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 21 | #include <signal.h> |
Andrew Jeffery | 04b81fc | 2020-02-05 13:07:29 +1030 | [diff] [blame] | 22 | #include <stdbool.h> |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 28 | #include <sys/signalfd.h> |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
| 31 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 32 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 33 | |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 34 | #if HAVE_SYSTEMD_SD_DAEMON_H |
| 35 | #include <systemd/sd-daemon.h> |
| 36 | #else |
| 37 | static inline int sd_listen_fds(int i __unused) |
| 38 | { |
| 39 | return -1; |
| 40 | } |
| 41 | #endif |
| 42 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 43 | static const mctp_eid_t local_eid_default = 8; |
| 44 | static char sockname[] = "\0mctp-mux"; |
| 45 | |
| 46 | struct binding { |
Andrew Jeffery | 8f53d63 | 2022-07-25 20:57:14 +0930 | [diff] [blame] | 47 | const char *name; |
| 48 | int (*init)(struct mctp *mctp, struct binding *binding, mctp_eid_t eid, |
| 49 | int n_params, char *const *params); |
| 50 | void (*destroy)(struct mctp *mctp, struct binding *binding); |
| 51 | int (*init_pollfd)(struct binding *binding, struct pollfd *pollfd); |
| 52 | int (*process)(struct binding *binding); |
| 53 | void *data; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct client { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 57 | bool active; |
| 58 | int sock; |
| 59 | uint8_t type; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct ctx { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 63 | struct mctp *mctp; |
| 64 | struct binding *binding; |
| 65 | bool verbose; |
| 66 | int local_eid; |
| 67 | void *buf; |
| 68 | size_t buf_size; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 69 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 70 | int sock; |
| 71 | struct pollfd *pollfds; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 72 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 73 | struct client *clients; |
| 74 | int n_clients; |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 75 | |
| 76 | struct { |
| 77 | struct capture binding; |
| 78 | struct capture socket; |
| 79 | } pcap; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static void tx_message(struct ctx *ctx, mctp_eid_t eid, void *msg, size_t len) |
| 83 | { |
Andrew Jeffery | 0673505 | 2021-01-27 23:50:55 +1030 | [diff] [blame] | 84 | int rc; |
| 85 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 86 | rc = mctp_message_tx(ctx->mctp, eid, MCTP_MESSAGE_TO_SRC, 0, msg, len); |
Andrew Jeffery | 0673505 | 2021-01-27 23:50:55 +1030 | [diff] [blame] | 87 | if (rc) |
| 88 | warnx("Failed to send message: %d", rc); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void client_remove_inactive(struct ctx *ctx) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | for (i = 0; i < ctx->n_clients; i++) { |
| 96 | struct client *client = &ctx->clients[i]; |
| 97 | if (client->active) |
| 98 | continue; |
| 99 | close(client->sock); |
| 100 | |
| 101 | ctx->n_clients--; |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 102 | memmove(&ctx->clients[i], &ctx->clients[i + 1], |
| 103 | (ctx->n_clients - i) * sizeof(*ctx->clients)); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 104 | ctx->clients = realloc(ctx->clients, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 105 | ctx->n_clients * sizeof(*ctx->clients)); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 109 | static void rx_message(uint8_t eid, bool tag_owner __unused, |
| 110 | uint8_t msg_tag __unused, void *data, void *msg, |
| 111 | size_t len) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 112 | { |
| 113 | struct ctx *ctx = data; |
| 114 | struct iovec iov[2]; |
| 115 | struct msghdr msghdr; |
| 116 | bool removed; |
| 117 | uint8_t type; |
| 118 | int i, rc; |
| 119 | |
| 120 | if (len < 2) |
| 121 | return; |
| 122 | |
| 123 | type = *(uint8_t *)msg; |
| 124 | |
| 125 | if (ctx->verbose) |
| 126 | fprintf(stderr, "MCTP message received: len %zd, type %d\n", |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 127 | len, type); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 128 | |
| 129 | memset(&msghdr, 0, sizeof(msghdr)); |
| 130 | msghdr.msg_iov = iov; |
| 131 | msghdr.msg_iovlen = 2; |
| 132 | iov[0].iov_base = &eid; |
| 133 | iov[0].iov_len = 1; |
| 134 | iov[1].iov_base = msg; |
| 135 | iov[1].iov_len = len; |
| 136 | |
| 137 | for (i = 0; i < ctx->n_clients; i++) { |
| 138 | struct client *client = &ctx->clients[i]; |
| 139 | |
| 140 | if (client->type != type) |
| 141 | continue; |
| 142 | |
| 143 | if (ctx->verbose) |
| 144 | fprintf(stderr, " forwarding to client %d\n", i); |
| 145 | |
| 146 | rc = sendmsg(client->sock, &msghdr, 0); |
| 147 | if (rc != (ssize_t)(len + 1)) { |
| 148 | client->active = false; |
| 149 | removed = true; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (removed) |
| 154 | client_remove_inactive(ctx); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 155 | } |
| 156 | |
Jeremy Kerr | 34b9b3d | 2019-11-27 11:27:05 +0800 | [diff] [blame] | 157 | static int binding_null_init(struct mctp *mctp __unused, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 158 | struct binding *binding __unused, |
| 159 | mctp_eid_t eid __unused, int n_params, |
| 160 | char *const *params __unused) |
Jeremy Kerr | 34b9b3d | 2019-11-27 11:27:05 +0800 | [diff] [blame] | 161 | { |
| 162 | if (n_params != 0) { |
| 163 | warnx("null binding doesn't accept parameters"); |
| 164 | return -1; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 169 | static int binding_serial_init(struct mctp *mctp, struct binding *binding, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 170 | mctp_eid_t eid, int n_params, |
| 171 | char *const *params) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 172 | { |
| 173 | struct mctp_binding_serial *serial; |
| 174 | const char *path; |
| 175 | int rc; |
| 176 | |
| 177 | if (n_params != 1) { |
| 178 | warnx("serial binding requires device param"); |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | path = params[0]; |
| 183 | |
| 184 | serial = mctp_serial_init(); |
| 185 | assert(serial); |
| 186 | |
| 187 | rc = mctp_serial_open_path(serial, path); |
| 188 | if (rc) |
| 189 | return -1; |
| 190 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 191 | mctp_register_bus(mctp, mctp_binding_serial_core(serial), eid); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 192 | |
| 193 | binding->data = serial; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Andrew Jeffery | 8f53d63 | 2022-07-25 20:57:14 +0930 | [diff] [blame] | 198 | static int binding_serial_init_pollfd(struct binding *binding, |
| 199 | struct pollfd *pollfd) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 200 | { |
Andrew Jeffery | 8f53d63 | 2022-07-25 20:57:14 +0930 | [diff] [blame] | 201 | return mctp_serial_init_pollfd(binding->data, pollfd); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int binding_serial_process(struct binding *binding) |
| 205 | { |
| 206 | return mctp_serial_read(binding->data); |
| 207 | } |
| 208 | |
| 209 | static int binding_astlpc_init(struct mctp *mctp, struct binding *binding, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 210 | mctp_eid_t eid, int n_params, |
| 211 | char *const *params __attribute__((unused))) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 212 | { |
| 213 | struct mctp_binding_astlpc *astlpc; |
Konstantin Aladyshev | 400766f | 2023-09-07 15:57:25 +0300 | [diff] [blame] | 214 | const char *path; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 215 | |
Konstantin Aladyshev | 400766f | 2023-09-07 15:57:25 +0300 | [diff] [blame] | 216 | if (n_params != 1) { |
| 217 | warnx("astlpc binding requires kcs device param"); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 218 | return -1; |
| 219 | } |
| 220 | |
Konstantin Aladyshev | 400766f | 2023-09-07 15:57:25 +0300 | [diff] [blame] | 221 | path = params[0]; |
| 222 | |
| 223 | astlpc = mctp_astlpc_init_fileio(path); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 224 | if (!astlpc) { |
| 225 | warnx("could not initialise astlpc binding"); |
| 226 | return -1; |
| 227 | } |
| 228 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 229 | mctp_register_bus(mctp, mctp_binding_astlpc_core(astlpc), eid); |
| 230 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 231 | binding->data = astlpc; |
| 232 | return 0; |
| 233 | } |
| 234 | |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 235 | static void binding_astlpc_destroy(struct mctp *mctp, struct binding *binding) |
| 236 | { |
| 237 | struct mctp_binding_astlpc *astlpc = binding->data; |
| 238 | |
| 239 | mctp_unregister_bus(mctp, mctp_binding_astlpc_core(astlpc)); |
| 240 | |
| 241 | mctp_astlpc_destroy(astlpc); |
| 242 | } |
| 243 | |
Andrew Jeffery | 8f53d63 | 2022-07-25 20:57:14 +0930 | [diff] [blame] | 244 | static int binding_astlpc_init_pollfd(struct binding *binding, |
| 245 | struct pollfd *pollfd) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 246 | { |
Andrew Jeffery | 8f53d63 | 2022-07-25 20:57:14 +0930 | [diff] [blame] | 247 | return mctp_astlpc_init_pollfd(binding->data, pollfd); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static int binding_astlpc_process(struct binding *binding) |
| 251 | { |
| 252 | return mctp_astlpc_poll(binding->data); |
| 253 | } |
| 254 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 255 | struct binding bindings[] = { { |
| 256 | .name = "null", |
| 257 | .init = binding_null_init, |
| 258 | }, |
| 259 | { |
| 260 | .name = "serial", |
| 261 | .init = binding_serial_init, |
| 262 | .destroy = NULL, |
| 263 | .init_pollfd = binding_serial_init_pollfd, |
| 264 | .process = binding_serial_process, |
| 265 | }, |
| 266 | { |
| 267 | .name = "astlpc", |
| 268 | .init = binding_astlpc_init, |
| 269 | .destroy = binding_astlpc_destroy, |
| 270 | .init_pollfd = binding_astlpc_init_pollfd, |
| 271 | .process = binding_astlpc_process, |
| 272 | } }; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 273 | |
| 274 | struct binding *binding_lookup(const char *name) |
| 275 | { |
| 276 | struct binding *binding; |
| 277 | unsigned int i; |
| 278 | |
| 279 | for (i = 0; i < ARRAY_SIZE(bindings); i++) { |
| 280 | binding = &bindings[i]; |
| 281 | |
| 282 | if (!strcmp(binding->name, name)) |
| 283 | return binding; |
| 284 | } |
| 285 | |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | static int socket_init(struct ctx *ctx) |
| 290 | { |
| 291 | struct sockaddr_un addr; |
| 292 | int namelen, rc; |
| 293 | |
| 294 | namelen = sizeof(sockname) - 1; |
| 295 | addr.sun_family = AF_UNIX; |
| 296 | memcpy(addr.sun_path, sockname, namelen); |
| 297 | |
| 298 | ctx->sock = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 299 | if (ctx->sock < 0) { |
| 300 | warn("can't create socket"); |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | rc = bind(ctx->sock, (struct sockaddr *)&addr, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 305 | sizeof(addr.sun_family) + namelen); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 306 | if (rc) { |
| 307 | warn("can't bind socket"); |
| 308 | goto err_close; |
| 309 | } |
| 310 | |
| 311 | rc = listen(ctx->sock, 1); |
| 312 | if (rc) { |
| 313 | warn("can't listen on socket"); |
| 314 | goto err_close; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | |
| 319 | err_close: |
| 320 | close(ctx->sock); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | static int socket_process(struct ctx *ctx) |
| 325 | { |
| 326 | struct client *client; |
| 327 | int fd; |
| 328 | |
| 329 | fd = accept4(ctx->sock, NULL, 0, SOCK_NONBLOCK); |
| 330 | if (fd < 0) |
| 331 | return -1; |
| 332 | |
| 333 | ctx->n_clients++; |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 334 | ctx->clients = |
| 335 | realloc(ctx->clients, ctx->n_clients * sizeof(struct client)); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 336 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 337 | client = &ctx->clients[ctx->n_clients - 1]; |
Andrew Jeffery | 8676c93 | 2020-01-24 12:22:21 +1030 | [diff] [blame] | 338 | memset(client, 0, sizeof(*client)); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 339 | client->active = true; |
| 340 | client->sock = fd; |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int client_process_recv(struct ctx *ctx, int idx) |
| 346 | { |
| 347 | struct client *client = &ctx->clients[idx]; |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 348 | uint8_t eid; |
| 349 | ssize_t len; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 350 | int rc; |
| 351 | |
| 352 | /* are we waiting for a type message? */ |
| 353 | if (!client->type) { |
| 354 | uint8_t type; |
| 355 | rc = read(client->sock, &type, 1); |
| 356 | if (rc <= 0) |
| 357 | goto out_close; |
| 358 | |
| 359 | if (type == 0) { |
| 360 | rc = -1; |
| 361 | goto out_close; |
| 362 | } |
| 363 | if (ctx->verbose) |
| 364 | fprintf(stderr, "client[%d] registered for type %u\n", |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 365 | idx, type); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 366 | client->type = type; |
| 367 | return 0; |
| 368 | } |
| 369 | |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 370 | len = recv(client->sock, NULL, 0, MSG_PEEK | MSG_TRUNC); |
| 371 | if (len < 0) { |
Andrew Jeffery | 6896d41 | 2020-03-11 09:25:32 +1030 | [diff] [blame] | 372 | if (errno != ECONNRESET) |
| 373 | warn("can't receive (peek) from client"); |
| 374 | |
Andrew Jeffery | 13a4041 | 2020-02-07 17:05:20 +1030 | [diff] [blame] | 375 | rc = -1; |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 376 | goto out_close; |
| 377 | } |
| 378 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 379 | if ((size_t)len > ctx->buf_size) { |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 380 | void *tmp; |
| 381 | |
| 382 | tmp = realloc(ctx->buf, len); |
| 383 | if (!tmp) { |
| 384 | warn("can't allocate for incoming message"); |
Andrew Jeffery | 13a4041 | 2020-02-07 17:05:20 +1030 | [diff] [blame] | 385 | rc = -1; |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 386 | goto out_close; |
| 387 | } |
| 388 | ctx->buf = tmp; |
| 389 | ctx->buf_size = len; |
| 390 | } |
| 391 | |
| 392 | rc = recv(client->sock, ctx->buf, ctx->buf_size, 0); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 393 | if (rc < 0) { |
Andrew Jeffery | 6896d41 | 2020-03-11 09:25:32 +1030 | [diff] [blame] | 394 | if (errno != ECONNRESET) |
| 395 | warn("can't receive from client"); |
Andrew Jeffery | 13a4041 | 2020-02-07 17:05:20 +1030 | [diff] [blame] | 396 | rc = -1; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 397 | goto out_close; |
| 398 | } |
| 399 | |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 400 | if (rc <= 0) { |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 401 | rc = -1; |
| 402 | goto out_close; |
| 403 | } |
| 404 | |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 405 | eid = *(uint8_t *)ctx->buf; |
Jeremy Kerr | d690d8e | 2019-08-01 21:16:58 +0800 | [diff] [blame] | 406 | |
Rashmica Gupta | f298897 | 2022-11-09 12:26:44 +1100 | [diff] [blame] | 407 | if (ctx->pcap.socket.path) |
| 408 | capture_socket(ctx->pcap.socket.dumper, ctx->buf, rc, |
| 409 | MCTP_MESSAGE_CAPTURE_OUTGOING, eid); |
| 410 | |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 411 | if (ctx->verbose) |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 412 | fprintf(stderr, "client[%d] sent message: dest 0x%02x len %d\n", |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 413 | idx, eid, rc - 1); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 414 | |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 415 | if (eid == ctx->local_eid) |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 416 | rx_message(eid, MCTP_MESSAGE_TO_DST, 0, ctx, ctx->buf + 1, |
| 417 | rc - 1); |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 418 | else |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 419 | tx_message(ctx, eid, ctx->buf + 1, rc - 1); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 420 | |
| 421 | return 0; |
| 422 | |
| 423 | out_close: |
| 424 | client->active = false; |
| 425 | return rc; |
| 426 | } |
| 427 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 428 | static int binding_init(struct ctx *ctx, const char *name, int argc, |
| 429 | char *const *argv) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 430 | { |
| 431 | int rc; |
| 432 | |
| 433 | ctx->binding = binding_lookup(name); |
| 434 | if (!ctx->binding) { |
| 435 | warnx("no such binding '%s'", name); |
| 436 | return -1; |
| 437 | } |
| 438 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 439 | rc = ctx->binding->init(ctx->mctp, ctx->binding, ctx->local_eid, argc, |
| 440 | argv); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 441 | return rc; |
| 442 | } |
| 443 | |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 444 | static void binding_destroy(struct ctx *ctx) |
| 445 | { |
| 446 | if (ctx->binding->destroy) |
| 447 | ctx->binding->destroy(ctx->mctp, ctx->binding); |
| 448 | } |
| 449 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 450 | enum { |
| 451 | FD_BINDING = 0, |
| 452 | FD_SOCKET, |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 453 | FD_SIGNAL, |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 454 | FD_NR, |
| 455 | }; |
| 456 | |
| 457 | static int run_daemon(struct ctx *ctx) |
| 458 | { |
| 459 | bool clients_changed = false; |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 460 | sigset_t mask; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 461 | int rc, i; |
| 462 | |
| 463 | ctx->pollfds = malloc(FD_NR * sizeof(struct pollfd)); |
| 464 | |
Andrew Jeffery | 8f53d63 | 2022-07-25 20:57:14 +0930 | [diff] [blame] | 465 | if (!ctx->binding->init_pollfd) { |
Jeremy Kerr | 34b9b3d | 2019-11-27 11:27:05 +0800 | [diff] [blame] | 466 | ctx->pollfds[FD_BINDING].fd = -1; |
| 467 | ctx->pollfds[FD_BINDING].events = 0; |
| 468 | } |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 469 | |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 470 | sigemptyset(&mask); |
| 471 | sigaddset(&mask, SIGINT); |
| 472 | sigaddset(&mask, SIGTERM); |
| 473 | sigaddset(&mask, SIGQUIT); |
| 474 | |
| 475 | if ((rc = sigprocmask(SIG_BLOCK, &mask, NULL)) == -1) { |
| 476 | warn("sigprocmask"); |
| 477 | return rc; |
| 478 | } |
| 479 | |
| 480 | ctx->pollfds[FD_SIGNAL].fd = signalfd(-1, &mask, 0); |
| 481 | ctx->pollfds[FD_SIGNAL].events = POLLIN; |
| 482 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 483 | ctx->pollfds[FD_SOCKET].fd = ctx->sock; |
| 484 | ctx->pollfds[FD_SOCKET].events = POLLIN; |
| 485 | |
| 486 | mctp_set_rx_all(ctx->mctp, rx_message, ctx); |
| 487 | |
| 488 | for (;;) { |
| 489 | if (clients_changed) { |
| 490 | int i; |
| 491 | |
| 492 | ctx->pollfds = realloc(ctx->pollfds, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 493 | (ctx->n_clients + FD_NR) * |
| 494 | sizeof(struct pollfd)); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 495 | |
| 496 | for (i = 0; i < ctx->n_clients; i++) { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 497 | ctx->pollfds[FD_NR + i].fd = |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 498 | ctx->clients[i].sock; |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 499 | ctx->pollfds[FD_NR + i].events = POLLIN; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 500 | } |
| 501 | clients_changed = false; |
| 502 | } |
| 503 | |
Andrew Jeffery | 8f53d63 | 2022-07-25 20:57:14 +0930 | [diff] [blame] | 504 | if (ctx->binding->init_pollfd) |
| 505 | ctx->binding->init_pollfd(ctx->binding, |
| 506 | &ctx->pollfds[FD_BINDING]); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 507 | rc = poll(ctx->pollfds, ctx->n_clients + FD_NR, -1); |
| 508 | if (rc < 0) { |
| 509 | warn("poll failed"); |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | if (!rc) |
| 514 | continue; |
| 515 | |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 516 | if (ctx->pollfds[FD_SIGNAL].revents) { |
| 517 | struct signalfd_siginfo si; |
| 518 | ssize_t got; |
| 519 | |
| 520 | got = read(ctx->pollfds[FD_SIGNAL].fd, &si, sizeof(si)); |
| 521 | if (got == sizeof(si)) { |
| 522 | warnx("Received %s, quitting", |
| 523 | strsignal(si.ssi_signo)); |
| 524 | rc = 0; |
| 525 | break; |
| 526 | } else { |
| 527 | warnx("Unexpected read result for signalfd: %d", |
| 528 | rc); |
| 529 | warnx("Quitting on the basis that signalfd became ready"); |
| 530 | rc = -1; |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 535 | if (ctx->pollfds[FD_BINDING].revents) { |
Jeremy Kerr | 34b9b3d | 2019-11-27 11:27:05 +0800 | [diff] [blame] | 536 | rc = 0; |
| 537 | if (ctx->binding->process) |
| 538 | rc = ctx->binding->process(ctx->binding); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 539 | if (rc) |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | for (i = 0; i < ctx->n_clients; i++) { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 544 | if (!ctx->pollfds[FD_NR + i].revents) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 545 | continue; |
| 546 | |
| 547 | rc = client_process_recv(ctx, i); |
| 548 | if (rc) |
| 549 | clients_changed = true; |
| 550 | } |
| 551 | |
| 552 | if (ctx->pollfds[FD_SOCKET].revents) { |
| 553 | rc = socket_process(ctx); |
| 554 | if (rc) |
| 555 | break; |
| 556 | clients_changed = true; |
| 557 | } |
| 558 | |
| 559 | if (clients_changed) |
| 560 | client_remove_inactive(ctx); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 561 | } |
| 562 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 563 | free(ctx->pollfds); |
| 564 | |
| 565 | return rc; |
| 566 | } |
| 567 | |
| 568 | static const struct option options[] = { |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 569 | { "capture-binding", required_argument, 0, 'b' }, |
| 570 | { "capture-socket", required_argument, 0, 's' }, |
| 571 | { "binding-linktype", required_argument, 0, 'B' }, |
| 572 | { "socket-linktype", required_argument, 0, 'S' }, |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 573 | { "verbose", no_argument, 0, 'v' }, |
| 574 | { "eid", required_argument, 0, 'e' }, |
| 575 | { 0 }, |
| 576 | }; |
| 577 | |
| 578 | static void usage(const char *progname) |
| 579 | { |
| 580 | unsigned int i; |
| 581 | |
| 582 | fprintf(stderr, "usage: %s <binding> [params]\n", progname); |
| 583 | fprintf(stderr, "Available bindings:\n"); |
| 584 | for (i = 0; i < ARRAY_SIZE(bindings); i++) |
| 585 | fprintf(stderr, " %s\n", bindings[i].name); |
| 586 | } |
| 587 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 588 | int main(int argc, char *const *argv) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 589 | { |
| 590 | struct ctx *ctx, _ctx; |
| 591 | int rc; |
| 592 | |
| 593 | ctx = &_ctx; |
| 594 | ctx->clients = NULL; |
| 595 | ctx->n_clients = 0; |
| 596 | ctx->local_eid = local_eid_default; |
Andrew Jeffery | 04b81fc | 2020-02-05 13:07:29 +1030 | [diff] [blame] | 597 | ctx->verbose = false; |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 598 | ctx->pcap.binding.path = NULL; |
| 599 | ctx->pcap.socket.path = NULL; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 600 | |
| 601 | for (;;) { |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 602 | rc = getopt_long(argc, argv, "b:es::v", options, NULL); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 603 | if (rc == -1) |
| 604 | break; |
| 605 | switch (rc) { |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 606 | case 'b': |
| 607 | ctx->pcap.binding.path = optarg; |
| 608 | break; |
| 609 | case 's': |
| 610 | ctx->pcap.socket.path = optarg; |
| 611 | break; |
| 612 | case 'B': |
Rashmica Gupta | f298897 | 2022-11-09 12:26:44 +1100 | [diff] [blame] | 613 | fprintf(stderr, |
| 614 | "binding-linktype argument is deprecated\n"); |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 615 | break; |
| 616 | case 'S': |
Rashmica Gupta | f298897 | 2022-11-09 12:26:44 +1100 | [diff] [blame] | 617 | fprintf(stderr, |
| 618 | "socket-linktype argument is deprecated\n"); |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 619 | break; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 620 | case 'v': |
| 621 | ctx->verbose = true; |
| 622 | break; |
| 623 | case 'e': |
| 624 | ctx->local_eid = atoi(optarg); |
| 625 | break; |
| 626 | default: |
| 627 | fprintf(stderr, "Invalid argument\n"); |
| 628 | return EXIT_FAILURE; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | if (optind >= argc) { |
| 633 | fprintf(stderr, "missing binding argument\n"); |
| 634 | usage(argv[0]); |
| 635 | return EXIT_FAILURE; |
| 636 | } |
| 637 | |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 638 | /* setup initial buffer */ |
| 639 | ctx->buf_size = 4096; |
| 640 | ctx->buf = malloc(ctx->buf_size); |
| 641 | |
Andrew Jeffery | dca8259 | 2023-06-23 15:02:05 +0930 | [diff] [blame] | 642 | mctp_set_log_stdio(ctx->verbose ? MCTP_LOG_DEBUG : MCTP_LOG_NOTICE); |
Jeremy Kerr | 0b278a6 | 2019-05-30 21:27:21 +0800 | [diff] [blame] | 643 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 644 | ctx->mctp = mctp_init(); |
| 645 | assert(ctx->mctp); |
| 646 | |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 647 | if (ctx->pcap.binding.path || ctx->pcap.socket.path) { |
| 648 | if (capture_init()) { |
| 649 | rc = EXIT_FAILURE; |
| 650 | goto cleanup_mctp; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (ctx->pcap.binding.path) { |
| 655 | rc = capture_prepare(&ctx->pcap.binding); |
| 656 | if (rc == -1) { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 657 | fprintf(stderr, "Failed to initialise capture: %d\n", |
| 658 | rc); |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 659 | rc = EXIT_FAILURE; |
| 660 | goto cleanup_mctp; |
| 661 | } |
| 662 | |
| 663 | mctp_set_capture_handler(ctx->mctp, capture_binding, |
| 664 | ctx->pcap.binding.dumper); |
| 665 | } |
| 666 | |
| 667 | if (ctx->pcap.socket.path) { |
| 668 | rc = capture_prepare(&ctx->pcap.socket); |
| 669 | if (rc == -1) { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 670 | fprintf(stderr, "Failed to initialise capture: %d\n", |
| 671 | rc); |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 672 | rc = EXIT_FAILURE; |
| 673 | goto cleanup_pcap_binding; |
| 674 | } |
| 675 | } |
| 676 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 677 | rc = binding_init(ctx, argv[optind], argc - optind - 1, |
| 678 | argv + optind + 1); |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 679 | if (rc) { |
| 680 | fprintf(stderr, "Failed to initialise binding: %d\n", rc); |
| 681 | rc = EXIT_FAILURE; |
| 682 | goto cleanup_pcap_socket; |
| 683 | } |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 684 | |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 685 | rc = sd_listen_fds(true); |
| 686 | if (rc <= 0) { |
| 687 | rc = socket_init(ctx); |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 688 | if (rc) { |
| 689 | fprintf(stderr, "Failed to initialse socket: %d\n", rc); |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 690 | goto cleanup_binding; |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 691 | } |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 692 | } else { |
| 693 | ctx->sock = SD_LISTEN_FDS_START; |
| 694 | } |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 695 | |
| 696 | rc = run_daemon(ctx); |
| 697 | |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 698 | cleanup_binding: |
| 699 | binding_destroy(ctx); |
| 700 | |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 701 | cleanup_pcap_socket: |
| 702 | if (ctx->pcap.socket.path) |
| 703 | capture_close(&ctx->pcap.socket); |
| 704 | |
| 705 | cleanup_pcap_binding: |
| 706 | if (ctx->pcap.binding.path) |
| 707 | capture_close(&ctx->pcap.binding); |
| 708 | |
| 709 | rc = rc ? EXIT_FAILURE : EXIT_SUCCESS; |
| 710 | cleanup_mctp: |
| 711 | |
| 712 | return rc; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 713 | } |