Patrick Williams | 691668f | 2023-11-01 08:19:10 -0500 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Andrew Jeffery | b0c1d20 | 2023-11-07 22:08:44 +1030 | [diff] [blame] | 2 | #include <libpldm/base.h> |
Andrew Jeffery | d12dd36 | 2023-11-10 15:36:38 +1030 | [diff] [blame] | 3 | #include <libpldm/pldm.h> |
Andrew Jeffery | b0c1d20 | 2023-11-07 22:08:44 +1030 | [diff] [blame] | 4 | #include <libpldm/transport.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 5 | |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 6 | #include <bits/types/struct_iovec.h> |
Andrew Jeffery | 4e1ba8a | 2023-06-29 10:48:33 +0930 | [diff] [blame] | 7 | #include <fcntl.h> |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 8 | #include <stdbool.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 9 | #include <stdlib.h> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 10 | #include <string.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 11 | #include <sys/socket.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 12 | #include <sys/un.h> |
| 13 | #include <unistd.h> |
| 14 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 15 | /* Temporary for old api */ |
Andrew Jeffery | efb4006 | 2023-11-10 13:48:39 +1030 | [diff] [blame] | 16 | #include <libpldm/transport/mctp-demux.h> |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 17 | extern int |
| 18 | pldm_transport_mctp_demux_get_socket_fd(struct pldm_transport_mctp_demux *ctx); |
| 19 | extern struct pldm_transport_mctp_demux * |
| 20 | pldm_transport_mctp_demux_init_with_fd(int mctp_fd); |
| 21 | |
| 22 | /* --- old APIS written in terms of the new API -- */ |
| 23 | /* |
| 24 | * pldm_open returns the file descriptor to the MCTP socket, which needs to |
| 25 | * persist over api calls (so a consumer can poll it for incoming messages). |
| 26 | * So we need a global variable to store the transport struct |
| 27 | */ |
| 28 | static struct pldm_transport_mctp_demux *open_transport; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 29 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 30 | LIBPLDM_ABI_DEPRECATED |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 31 | pldm_requester_rc_t pldm_open(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 32 | { |
Andrew Jeffery | 4e1ba8a | 2023-06-29 10:48:33 +0930 | [diff] [blame] | 33 | int fd = PLDM_REQUESTER_OPEN_FAIL; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 34 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 35 | if (open_transport) { |
Rashmica Gupta | 39f8832 | 2023-05-12 15:54:12 +1000 | [diff] [blame] | 36 | fd = pldm_transport_mctp_demux_get_socket_fd(open_transport); |
Andrew Jeffery | 4e1ba8a | 2023-06-29 10:48:33 +0930 | [diff] [blame] | 37 | |
| 38 | /* If someone has externally issued close() on fd then we need to start again. Use |
| 39 | * `fcntl(..., F_GETFD)` to test whether fd is valid. */ |
| 40 | if (fd < 0 || fcntl(fd, F_GETFD) < 0) { |
| 41 | pldm_close(); |
| 42 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 43 | } |
| 44 | |
Andrew Jeffery | 4e1ba8a | 2023-06-29 10:48:33 +0930 | [diff] [blame] | 45 | /* We retest open_transport as it may have been set to NULL by pldm_close() above. */ |
| 46 | if (!open_transport) { |
| 47 | struct pldm_transport_mctp_demux *demux = NULL; |
| 48 | |
| 49 | if (pldm_transport_mctp_demux_init(&demux) < 0) { |
| 50 | return PLDM_REQUESTER_OPEN_FAIL; |
| 51 | } |
| 52 | |
| 53 | open_transport = demux; |
| 54 | |
| 55 | fd = pldm_transport_mctp_demux_get_socket_fd(open_transport); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 56 | } |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 57 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 58 | return fd; |
| 59 | } |
| 60 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 61 | /* This macro does the setup and teardown required for the old API to use the |
| 62 | * new API. Since the setup/teardown logic is the same for all four send/recv |
| 63 | * functions, it makes sense to only define it once. */ |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 64 | #define PLDM_REQ_FN(eid, fd, fn, rc, ...) \ |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 65 | do { \ |
| 66 | struct pldm_transport_mctp_demux *demux; \ |
| 67 | bool using_open_transport = false; \ |
Rashmica Gupta | d10c6b0 | 2023-07-24 13:09:48 +1000 | [diff] [blame] | 68 | pldm_tid_t tid = eid; \ |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 69 | struct pldm_transport *ctx; \ |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 70 | /* The fd can be for a socket we opened or one the consumer \ |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 71 | * opened. */ \ |
| 72 | if (open_transport && \ |
| 73 | mctp_fd == pldm_transport_mctp_demux_get_socket_fd( \ |
| 74 | open_transport)) { \ |
| 75 | using_open_transport = true; \ |
| 76 | demux = open_transport; \ |
| 77 | } else { \ |
| 78 | demux = pldm_transport_mctp_demux_init_with_fd(fd); \ |
| 79 | if (!demux) { \ |
| 80 | rc = PLDM_REQUESTER_OPEN_FAIL; \ |
| 81 | goto transport_out; \ |
| 82 | } \ |
| 83 | } \ |
| 84 | ctx = pldm_transport_mctp_demux_core(demux); \ |
| 85 | rc = pldm_transport_mctp_demux_map_tid(demux, tid, eid); \ |
| 86 | if (rc) { \ |
| 87 | rc = PLDM_REQUESTER_OPEN_FAIL; \ |
| 88 | goto transport_out; \ |
| 89 | } \ |
| 90 | rc = fn(ctx, tid, __VA_ARGS__); \ |
| 91 | transport_out: \ |
| 92 | if (!using_open_transport) { \ |
| 93 | pldm_transport_mctp_demux_destroy(demux); \ |
| 94 | } \ |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 95 | break; \ |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 96 | } while (0) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 97 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 98 | LIBPLDM_ABI_DEPRECATED |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 99 | pldm_requester_rc_t pldm_recv_any(mctp_eid_t eid, int mctp_fd, |
| 100 | uint8_t **pldm_resp_msg, size_t *resp_msg_len) |
| 101 | { |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 102 | pldm_requester_rc_t rc = 0; |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 103 | |
| 104 | struct pldm_transport_mctp_demux *demux; |
| 105 | bool using_open_transport = false; |
| 106 | pldm_tid_t tid = eid; |
| 107 | struct pldm_transport *ctx; |
| 108 | /* The fd can be for a socket we opened or one the consumer |
| 109 | * opened. */ |
| 110 | if (open_transport && |
| 111 | mctp_fd == |
| 112 | pldm_transport_mctp_demux_get_socket_fd(open_transport)) { |
| 113 | using_open_transport = true; |
| 114 | demux = open_transport; |
| 115 | } else { |
| 116 | demux = pldm_transport_mctp_demux_init_with_fd(mctp_fd); |
| 117 | if (!demux) { |
| 118 | rc = PLDM_REQUESTER_OPEN_FAIL; |
| 119 | goto transport_out; |
| 120 | } |
| 121 | } |
| 122 | ctx = pldm_transport_mctp_demux_core(demux); |
| 123 | rc = pldm_transport_mctp_demux_map_tid(demux, tid, eid); |
| 124 | if (rc) { |
| 125 | rc = PLDM_REQUESTER_OPEN_FAIL; |
| 126 | goto transport_out; |
| 127 | } |
| 128 | /* TODO this is the only change, can we work this into the macro? */ |
| 129 | rc = pldm_transport_recv_msg(ctx, &tid, (void **)pldm_resp_msg, |
| 130 | resp_msg_len); |
| 131 | |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 132 | struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)(*pldm_resp_msg); |
| 133 | if (rc != PLDM_REQUESTER_SUCCESS) { |
| 134 | return rc; |
| 135 | } |
| 136 | if (hdr && (hdr->request || hdr->datagram)) { |
| 137 | free(*pldm_resp_msg); |
| 138 | *pldm_resp_msg = NULL; |
| 139 | return PLDM_REQUESTER_NOT_RESP_MSG; |
| 140 | } |
| 141 | uint8_t pldm_cc = 0; |
| 142 | if (*resp_msg_len < (sizeof(struct pldm_msg_hdr) + sizeof(pldm_cc))) { |
| 143 | free(*pldm_resp_msg); |
| 144 | *pldm_resp_msg = NULL; |
| 145 | return PLDM_REQUESTER_RESP_MSG_TOO_SMALL; |
| 146 | } |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 147 | |
| 148 | transport_out: |
| 149 | if (!using_open_transport) { |
| 150 | pldm_transport_mctp_demux_destroy(demux); |
| 151 | } |
| 152 | |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 153 | return rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 154 | } |
| 155 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 156 | LIBPLDM_ABI_DEPRECATED |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 157 | pldm_requester_rc_t pldm_recv(mctp_eid_t eid, int mctp_fd, |
| 158 | __attribute__((unused)) uint8_t instance_id, |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 159 | uint8_t **pldm_resp_msg, size_t *resp_msg_len) |
| 160 | { |
| 161 | pldm_requester_rc_t rc = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 162 | pldm_recv_any(eid, mctp_fd, pldm_resp_msg, resp_msg_len); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 163 | struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)(*pldm_resp_msg); |
Rashmica Gupta | a303593 | 2023-06-17 12:52:51 +1000 | [diff] [blame] | 164 | if (rc == PLDM_REQUESTER_SUCCESS && hdr && |
| 165 | hdr->instance_id != instance_id) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 166 | free(*pldm_resp_msg); |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 167 | *pldm_resp_msg = NULL; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 168 | return PLDM_REQUESTER_INSTANCE_ID_MISMATCH; |
| 169 | } |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 170 | return rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 171 | } |
| 172 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 173 | LIBPLDM_ABI_DEPRECATED |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 174 | pldm_requester_rc_t pldm_send_recv(mctp_eid_t eid, int mctp_fd, |
| 175 | const uint8_t *pldm_req_msg, |
| 176 | size_t req_msg_len, uint8_t **pldm_resp_msg, |
| 177 | size_t *resp_msg_len) |
| 178 | { |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 179 | pldm_requester_rc_t rc = 0; |
| 180 | struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)pldm_req_msg; |
| 181 | if (hdr && !hdr->request) { |
| 182 | return PLDM_REQUESTER_NOT_REQ_MSG; |
| 183 | } |
| 184 | PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_recv_msg, rc, |
| 185 | pldm_req_msg, req_msg_len, (void **)pldm_resp_msg, |
| 186 | resp_msg_len); |
| 187 | if (rc != PLDM_REQUESTER_SUCCESS) { |
| 188 | return rc; |
| 189 | } |
Thu Nguyen | 43a7985 | 2023-07-17 16:22:11 +0700 | [diff] [blame] | 190 | hdr = (struct pldm_msg_hdr *)(*pldm_resp_msg); |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 191 | if (hdr && (hdr->request || hdr->datagram)) { |
| 192 | free(*pldm_resp_msg); |
| 193 | *pldm_resp_msg = NULL; |
| 194 | return PLDM_REQUESTER_NOT_RESP_MSG; |
| 195 | } |
| 196 | return rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 197 | } |
| 198 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 199 | LIBPLDM_ABI_DEPRECATED |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 200 | pldm_requester_rc_t pldm_send(mctp_eid_t eid, int mctp_fd, |
| 201 | const uint8_t *pldm_req_msg, size_t req_msg_len) |
| 202 | { |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 203 | pldm_requester_rc_t rc = 0; |
| 204 | struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)pldm_req_msg; |
| 205 | if (!hdr->request) { |
| 206 | return PLDM_REQUESTER_NOT_REQ_MSG; |
| 207 | } |
| 208 | PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_msg, rc, |
| 209 | (void *)pldm_req_msg, req_msg_len); |
| 210 | return rc; |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 211 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 212 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 213 | /* Adding this here for completeness in the case we can't smoothly |
| 214 | * transition apps over to the new api */ |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 215 | LIBPLDM_ABI_DEPRECATED |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 216 | void pldm_close(void) |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 217 | { |
| 218 | if (open_transport) { |
| 219 | pldm_transport_mctp_demux_destroy(open_transport); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 220 | } |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 221 | open_transport = NULL; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 222 | } |