Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1 | #include "mctp-defines.h" |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 2 | #include "base.h" |
| 3 | #include "container-of.h" |
| 4 | #include "libpldm/pldm.h" |
| 5 | #include "libpldm/transport.h" |
BonnieLo-wiwynn | 36402ff | 2023-08-16 15:38:51 +0800 | [diff] [blame] | 6 | #include "libpldm/transport/af-mctp.h" |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 7 | #include "socket.h" |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 8 | #include "transport.h" |
| 9 | |
| 10 | #include <errno.h> |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 11 | #include <limits.h> |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 12 | #include <linux/mctp.h> |
| 13 | #include <poll.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/un.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #define AF_MCTP_NAME "AF_MCTP" |
| 22 | struct pldm_transport_af_mctp { |
| 23 | struct pldm_transport transport; |
| 24 | int socket; |
| 25 | pldm_tid_t tid_eid_map[MCTP_MAX_NUM_EID]; |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 26 | struct pldm_socket_sndbuf socket_send_buf; |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | #define transport_to_af_mctp(ptr) \ |
| 30 | container_of(ptr, struct pldm_transport_af_mctp, transport) |
| 31 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 32 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 33 | struct pldm_transport * |
| 34 | pldm_transport_af_mctp_core(struct pldm_transport_af_mctp *ctx) |
| 35 | { |
| 36 | return &ctx->transport; |
| 37 | } |
| 38 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 39 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 40 | int pldm_transport_af_mctp_init_pollfd(struct pldm_transport *t, |
| 41 | struct pollfd *pollfd) |
| 42 | { |
| 43 | struct pldm_transport_af_mctp *ctx = transport_to_af_mctp(t); |
| 44 | pollfd->fd = ctx->socket; |
| 45 | pollfd->events = POLLIN; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int pldm_transport_af_mctp_get_eid(struct pldm_transport_af_mctp *ctx, |
| 50 | pldm_tid_t tid, mctp_eid_t *eid) |
| 51 | { |
| 52 | int i; |
| 53 | for (i = 0; i < MCTP_MAX_NUM_EID; i++) { |
| 54 | if (ctx->tid_eid_map[i] == tid) { |
| 55 | *eid = i; |
| 56 | return 0; |
| 57 | } |
| 58 | } |
| 59 | *eid = -1; |
| 60 | return -1; |
| 61 | } |
| 62 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 63 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 64 | int pldm_transport_af_mctp_map_tid(struct pldm_transport_af_mctp *ctx, |
| 65 | pldm_tid_t tid, mctp_eid_t eid) |
| 66 | { |
| 67 | ctx->tid_eid_map[eid] = tid; |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 72 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 73 | int pldm_transport_af_mctp_unmap_tid(struct pldm_transport_af_mctp *ctx, |
| 74 | __attribute__((unused)) pldm_tid_t tid, |
| 75 | mctp_eid_t eid) |
| 76 | { |
| 77 | ctx->tid_eid_map[eid] = 0; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static pldm_requester_rc_t pldm_transport_af_mctp_recv(struct pldm_transport *t, |
| 83 | pldm_tid_t tid, |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 84 | void **pldm_msg, |
| 85 | size_t *msg_len) |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 86 | { |
| 87 | struct pldm_transport_af_mctp *af_mctp = transport_to_af_mctp(t); |
| 88 | mctp_eid_t eid = 0; |
Andrew Jeffery | 2a6a342 | 2023-08-18 09:53:21 +0930 | [diff] [blame] | 89 | ssize_t length; |
Andrew Jeffery | ac24737 | 2023-08-18 10:46:12 +0930 | [diff] [blame] | 90 | void *msg; |
Andrew Jeffery | 2a6a342 | 2023-08-18 09:53:21 +0930 | [diff] [blame] | 91 | int rc; |
| 92 | |
| 93 | rc = pldm_transport_af_mctp_get_eid(af_mctp, tid, &eid); |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 94 | if (rc) { |
| 95 | return PLDM_REQUESTER_RECV_FAIL; |
| 96 | } |
| 97 | |
Andrew Jeffery | 2a6a342 | 2023-08-18 09:53:21 +0930 | [diff] [blame] | 98 | length = recv(af_mctp->socket, NULL, 0, MSG_PEEK | MSG_TRUNC); |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 99 | if (length <= 0) { |
| 100 | return PLDM_REQUESTER_RECV_FAIL; |
| 101 | } |
Andrew Jeffery | 2a6a342 | 2023-08-18 09:53:21 +0930 | [diff] [blame] | 102 | |
Andrew Jeffery | ac24737 | 2023-08-18 10:46:12 +0930 | [diff] [blame] | 103 | msg = malloc(length); |
Andrew Jeffery | 7caa8af | 2023-08-18 10:50:54 +0930 | [diff] [blame^] | 104 | if (!msg) { |
| 105 | return PLDM_REQUESTER_RECV_FAIL; |
| 106 | } |
| 107 | |
Andrew Jeffery | ac24737 | 2023-08-18 10:46:12 +0930 | [diff] [blame] | 108 | length = recv(af_mctp->socket, msg, length, MSG_TRUNC); |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 109 | if (length < (ssize_t)sizeof(struct pldm_msg_hdr)) { |
Andrew Jeffery | ac24737 | 2023-08-18 10:46:12 +0930 | [diff] [blame] | 110 | free(msg); |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 111 | return PLDM_REQUESTER_INVALID_RECV_LEN; |
| 112 | } |
Andrew Jeffery | 2a6a342 | 2023-08-18 09:53:21 +0930 | [diff] [blame] | 113 | |
Andrew Jeffery | ac24737 | 2023-08-18 10:46:12 +0930 | [diff] [blame] | 114 | *pldm_msg = msg; |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 115 | *msg_len = length; |
| 116 | |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 117 | return PLDM_REQUESTER_SUCCESS; |
| 118 | } |
| 119 | |
| 120 | static pldm_requester_rc_t pldm_transport_af_mctp_send(struct pldm_transport *t, |
| 121 | pldm_tid_t tid, |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 122 | const void *pldm_msg, |
| 123 | size_t msg_len) |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 124 | { |
| 125 | struct pldm_transport_af_mctp *af_mctp = transport_to_af_mctp(t); |
| 126 | mctp_eid_t eid = 0; |
| 127 | if (pldm_transport_af_mctp_get_eid(af_mctp, tid, &eid)) { |
| 128 | return PLDM_REQUESTER_SEND_FAIL; |
| 129 | } |
| 130 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 131 | struct sockaddr_mctp addr = { 0 }; |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 132 | addr.smctp_family = AF_MCTP; |
| 133 | addr.smctp_addr.s_addr = eid; |
| 134 | addr.smctp_type = MCTP_MSG_TYPE_PLDM; |
| 135 | addr.smctp_tag = MCTP_TAG_OWNER; |
| 136 | |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 137 | if (msg_len > INT_MAX || |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 138 | pldm_socket_sndbuf_accomodate(&(af_mctp->socket_send_buf), |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 139 | (int)msg_len)) { |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 140 | return PLDM_REQUESTER_SEND_FAIL; |
| 141 | } |
| 142 | |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 143 | ssize_t rc = sendto(af_mctp->socket, pldm_msg, msg_len, 0, |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 144 | (struct sockaddr *)&addr, sizeof(addr)); |
| 145 | if (rc == -1) { |
| 146 | return PLDM_REQUESTER_SEND_FAIL; |
| 147 | } |
| 148 | return PLDM_REQUESTER_SUCCESS; |
| 149 | } |
| 150 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 151 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 152 | int pldm_transport_af_mctp_init(struct pldm_transport_af_mctp **ctx) |
| 153 | { |
| 154 | if (!ctx || *ctx) { |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | |
| 158 | struct pldm_transport_af_mctp *af_mctp = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 159 | calloc(1, sizeof(struct pldm_transport_af_mctp)); |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 160 | if (!af_mctp) { |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | |
| 164 | af_mctp->transport.name = AF_MCTP_NAME; |
| 165 | af_mctp->transport.version = 1; |
| 166 | af_mctp->transport.recv = pldm_transport_af_mctp_recv; |
| 167 | af_mctp->transport.send = pldm_transport_af_mctp_send; |
Delphine CC Chiu | b4cd517 | 2023-08-15 11:30:44 +0800 | [diff] [blame] | 168 | af_mctp->transport.init_pollfd = pldm_transport_af_mctp_init_pollfd; |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 169 | af_mctp->socket = socket(AF_MCTP, SOCK_DGRAM, 0); |
| 170 | if (af_mctp->socket == -1) { |
| 171 | free(af_mctp); |
| 172 | return -1; |
| 173 | } |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 174 | |
| 175 | if (pldm_socket_sndbuf_init(&af_mctp->socket_send_buf, |
| 176 | af_mctp->socket)) { |
| 177 | close(af_mctp->socket); |
| 178 | free(af_mctp); |
| 179 | return -1; |
| 180 | } |
| 181 | |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 182 | *ctx = af_mctp; |
| 183 | return 0; |
| 184 | } |
| 185 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 186 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 187 | void pldm_transport_af_mctp_destroy(struct pldm_transport_af_mctp *ctx) |
| 188 | { |
| 189 | if (!ctx) { |
| 190 | return; |
| 191 | } |
| 192 | close(ctx->socket); |
| 193 | free(ctx); |
| 194 | } |