Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #include "aos/network/sctp_lib.h" |
| 2 | |
| 3 | #include <arpa/inet.h> |
Adam Snaider | be26351 | 2023-05-18 20:40:23 -0700 | [diff] [blame] | 4 | #include <linux/sctp.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 5 | #include <net/if.h> |
| 6 | #include <netdb.h> |
Philipp Schrader | d2a5f5d | 2023-12-22 11:25:04 -0800 | [diff] [blame^] | 7 | #include <netinet/ip.h> |
Adam Snaider | be26351 | 2023-05-18 20:40:23 -0700 | [diff] [blame] | 8 | #include <sys/socket.h> |
Austin Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 9 | #include <sys/stat.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <unistd.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 12 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 14 | #include <cerrno> |
| 15 | #include <fstream> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 16 | #include <string_view> |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 17 | #include <vector> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 18 | |
Austin Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 19 | #include "aos/util/file.h" |
| 20 | |
Adam Snaider | 13d48d9 | 2023-08-03 12:20:15 -0700 | [diff] [blame] | 21 | // The casts required to read datastructures from sockets trip - Wcast - align. |
| 22 | #ifdef __clang |
| 23 | #pragma clang diagnostic ignored "-Wcast-align" |
| 24 | #endif |
| 25 | |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 26 | DEFINE_string(interface, "", "network interface"); |
Brian Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 27 | DEFINE_bool(disable_ipv6, false, "disable ipv6"); |
Austin Schuh | 9dd8f59 | 2021-12-25 14:32:43 -0800 | [diff] [blame] | 28 | DEFINE_int32(rmem, 0, "If nonzero, set rmem to this size."); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 29 | |
Philipp Schrader | d2a5f5d | 2023-12-22 11:25:04 -0800 | [diff] [blame^] | 30 | // The Type of Service. |
| 31 | // https://www.tucny.com/Home/dscp-tos |
| 32 | // |
| 33 | // We want to set the highest precedence (i.e. critical) with minimal delay. We |
| 34 | // also want to be able to stuff the packets into bucket 0 for queue |
| 35 | // disciplining. Experiments show that 176 works for this. Other values (e.g. |
| 36 | // DSCP class EF) cannot be stuffed into bucket 0 (for unknown reasons). |
| 37 | // |
| 38 | // Note that the two least significant bits are reserved and should always set |
| 39 | // to zero. Those two bits are the "Explicit Congestion Notification" bits. They |
| 40 | // are controlled by the IP stack itself (and used by the router). We don't |
| 41 | // control that via the TOS value we set here. |
| 42 | DEFINE_int32( |
| 43 | sctp_tos, 176, |
| 44 | "The Type-Of-Service value to use. Defaults to a critical priority. " |
| 45 | "Always set values here whose two least significant bits are set to zero. " |
| 46 | "When using tcpdump, the `tos` field may show the least significant two " |
| 47 | "bits set to something other than zero."); |
| 48 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 49 | namespace aos::message_bridge { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 50 | |
| 51 | namespace { |
| 52 | const char *sac_state_tbl[] = {"COMMUNICATION_UP", "COMMUNICATION_LOST", |
| 53 | "RESTART", "SHUTDOWN_COMPLETE", |
Sarah Newman | 4aeb237 | 2022-04-06 13:07:11 -0700 | [diff] [blame] | 54 | "CANT_START_ASSOCIATION"}; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 55 | |
| 56 | typedef union { |
| 57 | struct sctp_initmsg init; |
| 58 | struct sctp_sndrcvinfo sndrcvinfo; |
| 59 | } _sctp_cmsg_data_t; |
| 60 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 61 | #if HAS_SCTP_AUTH |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 62 | // Returns true if SCTP authentication is available and enabled. |
| 63 | bool SctpAuthIsEnabled() { |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 64 | struct stat current_stat; |
| 65 | if (stat("/proc/sys/net/sctp/auth_enable", ¤t_stat) != -1) { |
| 66 | int value = std::stoi( |
| 67 | util::ReadFileToStringOrDie("/proc/sys/net/sctp/auth_enable")); |
| 68 | CHECK(value == 0 || value == 1) |
| 69 | << "Unknown auth enable sysctl value: " << value; |
| 70 | return value == 1; |
| 71 | } else { |
| 72 | LOG(WARNING) << "/proc/sys/net/sctp/auth_enable doesn't exist."; |
| 73 | return false; |
| 74 | } |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 77 | std::vector<uint8_t> GenerateSecureRandomSequence(size_t count) { |
| 78 | std::ifstream rng("/dev/random", std::ios::in | std::ios::binary); |
| 79 | CHECK(rng) << "Unable to open /dev/random"; |
| 80 | std::vector<uint8_t> out(count, 0); |
| 81 | rng.read(reinterpret_cast<char *>(out.data()), count); |
| 82 | CHECK(rng) << "Couldn't read from random device"; |
| 83 | rng.close(); |
| 84 | return out; |
| 85 | } |
| 86 | #endif |
| 87 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 88 | } // namespace |
| 89 | |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 90 | bool Ipv6Enabled() { |
| 91 | if (FLAGS_disable_ipv6) { |
| 92 | return false; |
| 93 | } |
| 94 | int fd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 95 | if (fd != -1) { |
| 96 | close(fd); |
| 97 | return true; |
| 98 | } |
| 99 | switch (errno) { |
| 100 | case EAFNOSUPPORT: |
| 101 | case EINVAL: |
| 102 | case EPROTONOSUPPORT: |
| 103 | PLOG(INFO) << "no ipv6"; |
| 104 | return false; |
| 105 | default: |
| 106 | PLOG(FATAL) << "Open socket failed"; |
| 107 | return false; |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | struct sockaddr_storage ResolveSocket(std::string_view host, int port, |
| 112 | bool use_ipv6) { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 113 | struct sockaddr_storage result; |
James Kuszmaul | 784deb7 | 2023-02-17 14:42:51 -0800 | [diff] [blame] | 114 | memset(&result, 0, sizeof(result)); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 115 | struct addrinfo *addrinfo_result; |
| 116 | struct sockaddr_in *t_addr = (struct sockaddr_in *)&result; |
| 117 | struct sockaddr_in6 *t_addr6 = (struct sockaddr_in6 *)&result; |
Brian Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 118 | struct addrinfo hints; |
| 119 | memset(&hints, 0, sizeof(hints)); |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 120 | if (!use_ipv6) { |
Brian Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 121 | hints.ai_family = AF_INET; |
| 122 | } else { |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 123 | // Default to IPv6 as the clearly superior protocol, since it also handles |
| 124 | // IPv4. |
Brian Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 125 | hints.ai_family = AF_INET6; |
| 126 | } |
| 127 | hints.ai_socktype = SOCK_SEQPACKET; |
| 128 | hints.ai_protocol = IPPROTO_SCTP; |
| 129 | // We deliberately avoid AI_ADDRCONFIG here because it breaks running things |
| 130 | // inside Bazel's test sandbox, which has no non-localhost IPv4 or IPv6 |
| 131 | // addresses. Also, it's not really helpful, because most systems will have |
| 132 | // link-local addresses of both types with any interface that's up. |
| 133 | hints.ai_flags = AI_PASSIVE | AI_V4MAPPED | AI_NUMERICSERV; |
| 134 | int ret = getaddrinfo(host.empty() ? nullptr : std::string(host).c_str(), |
| 135 | std::to_string(port).c_str(), &hints, &addrinfo_result); |
Brian Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 136 | if (ret == EAI_SYSTEM) { |
| 137 | PLOG(FATAL) << "getaddrinfo failed to look up '" << host << "'"; |
| 138 | } else if (ret != 0) { |
| 139 | LOG(FATAL) << "getaddrinfo failed to look up '" << host |
| 140 | << "': " << gai_strerror(ret); |
| 141 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 142 | switch (addrinfo_result->ai_family) { |
| 143 | case AF_INET: |
| 144 | memcpy(t_addr, addrinfo_result->ai_addr, addrinfo_result->ai_addrlen); |
| 145 | t_addr->sin_family = addrinfo_result->ai_family; |
| 146 | t_addr->sin_port = htons(port); |
| 147 | |
| 148 | break; |
| 149 | case AF_INET6: |
| 150 | memcpy(t_addr6, addrinfo_result->ai_addr, addrinfo_result->ai_addrlen); |
| 151 | t_addr6->sin6_family = addrinfo_result->ai_family; |
| 152 | t_addr6->sin6_port = htons(port); |
| 153 | |
| 154 | if (FLAGS_interface.size() > 0) { |
| 155 | t_addr6->sin6_scope_id = if_nametoindex(FLAGS_interface.c_str()); |
| 156 | } |
| 157 | |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | // Now print it back out nicely. |
| 162 | char host_string[NI_MAXHOST]; |
| 163 | char service_string[NI_MAXSERV]; |
| 164 | |
| 165 | int error = getnameinfo((struct sockaddr *)&result, |
| 166 | addrinfo_result->ai_addrlen, host_string, NI_MAXHOST, |
| 167 | service_string, NI_MAXSERV, NI_NUMERICHOST); |
| 168 | |
| 169 | if (error) { |
| 170 | LOG(ERROR) << "Reverse lookup failed ... " << gai_strerror(error); |
| 171 | } |
| 172 | |
| 173 | LOG(INFO) << "remote:addr=" << host_string << ", port=" << service_string |
| 174 | << ", family=" << addrinfo_result->ai_family; |
| 175 | |
| 176 | freeaddrinfo(addrinfo_result); |
| 177 | |
| 178 | return result; |
| 179 | } |
| 180 | |
| 181 | std::string_view Family(const struct sockaddr_storage &sockaddr) { |
| 182 | if (sockaddr.ss_family == AF_INET) { |
| 183 | return "AF_INET"; |
| 184 | } else if (sockaddr.ss_family == AF_INET6) { |
| 185 | return "AF_INET6"; |
| 186 | } else { |
| 187 | return "unknown"; |
| 188 | } |
| 189 | } |
| 190 | std::string Address(const struct sockaddr_storage &sockaddr) { |
| 191 | char addrbuf[INET6_ADDRSTRLEN]; |
| 192 | if (sockaddr.ss_family == AF_INET) { |
| 193 | const struct sockaddr_in *sin = (const struct sockaddr_in *)&sockaddr; |
| 194 | return std::string( |
| 195 | inet_ntop(AF_INET, &sin->sin_addr, addrbuf, INET6_ADDRSTRLEN)); |
| 196 | } else { |
| 197 | const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)&sockaddr; |
| 198 | return std::string( |
| 199 | inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, INET6_ADDRSTRLEN)); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void PrintNotification(const Message *msg) { |
| 204 | const union sctp_notification *snp = |
| 205 | (const union sctp_notification *)msg->data(); |
| 206 | |
| 207 | LOG(INFO) << "Notification:"; |
| 208 | |
| 209 | switch (snp->sn_header.sn_type) { |
| 210 | case SCTP_ASSOC_CHANGE: { |
| 211 | const struct sctp_assoc_change *sac = &snp->sn_assoc_change; |
| 212 | LOG(INFO) << "SCTP_ASSOC_CHANGE(" << sac_state_tbl[sac->sac_state] << ")"; |
| 213 | VLOG(1) << " (assoc_change: state=" << sac->sac_state |
| 214 | << ", error=" << sac->sac_error |
| 215 | << ", instr=" << sac->sac_inbound_streams |
| 216 | << " outstr=" << sac->sac_outbound_streams |
| 217 | << ", assoc=" << sac->sac_assoc_id << ")"; |
| 218 | } break; |
| 219 | case SCTP_PEER_ADDR_CHANGE: { |
| 220 | const struct sctp_paddr_change *spc = &snp->sn_paddr_change; |
| 221 | LOG(INFO) << " SlCTP_PEER_ADDR_CHANGE"; |
| 222 | VLOG(1) << "\t\t(peer_addr_change: " << Address(spc->spc_aaddr) |
| 223 | << " state=" << spc->spc_state << ", error=" << spc->spc_error |
| 224 | << ")"; |
| 225 | } break; |
| 226 | case SCTP_SEND_FAILED: { |
| 227 | const struct sctp_send_failed *ssf = &snp->sn_send_failed; |
| 228 | LOG(INFO) << " SCTP_SEND_FAILED"; |
| 229 | VLOG(1) << "\t\t(sendfailed: len=" << ssf->ssf_length |
| 230 | << " err=" << ssf->ssf_error << ")"; |
| 231 | } break; |
| 232 | case SCTP_REMOTE_ERROR: { |
| 233 | const struct sctp_remote_error *sre = &snp->sn_remote_error; |
| 234 | LOG(INFO) << " SCTP_REMOTE_ERROR"; |
| 235 | VLOG(1) << "\t\t(remote_error: err=" << ntohs(sre->sre_error) << ")"; |
| 236 | } break; |
Austin Schuh | f777700 | 2020-09-01 18:41:28 -0700 | [diff] [blame] | 237 | case SCTP_STREAM_CHANGE_EVENT: { |
Austin Schuh | 62a0c27 | 2021-03-31 21:04:53 -0700 | [diff] [blame] | 238 | const struct sctp_stream_change_event *sce = &snp->sn_strchange_event; |
Austin Schuh | f777700 | 2020-09-01 18:41:28 -0700 | [diff] [blame] | 239 | LOG(INFO) << " SCTP_STREAM_CHANGE_EVENT"; |
| 240 | VLOG(1) << "\t\t(stream_change_event: flags=" << sce->strchange_flags |
| 241 | << ", assoc_id=" << sce->strchange_assoc_id |
| 242 | << ", instrms=" << sce->strchange_instrms |
| 243 | << ", outstrms=" << sce->strchange_outstrms << " )"; |
| 244 | } break; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 245 | case SCTP_SHUTDOWN_EVENT: { |
| 246 | LOG(INFO) << " SCTP_SHUTDOWN_EVENT"; |
| 247 | } break; |
| 248 | default: |
| 249 | LOG(INFO) << " Unknown type: " << snp->sn_header.sn_type; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | std::string GetHostname() { |
| 255 | char buf[256]; |
| 256 | buf[sizeof(buf) - 1] = '\0'; |
| 257 | PCHECK(gethostname(buf, sizeof(buf) - 1) == 0); |
| 258 | return buf; |
| 259 | } |
| 260 | |
| 261 | std::string Message::PeerAddress() const { return Address(sin); } |
| 262 | |
| 263 | void LogSctpStatus(int fd, sctp_assoc_t assoc_id) { |
| 264 | struct sctp_status status; |
| 265 | memset(&status, 0, sizeof(status)); |
| 266 | status.sstat_assoc_id = assoc_id; |
| 267 | |
| 268 | socklen_t size = sizeof(status); |
Adam Snaider | be26351 | 2023-05-18 20:40:23 -0700 | [diff] [blame] | 269 | const int result = getsockopt(fd, IPPROTO_SCTP, SCTP_STATUS, |
Austin Schuh | a5f545b | 2021-07-31 20:39:42 -0700 | [diff] [blame] | 270 | reinterpret_cast<void *>(&status), &size); |
| 271 | if (result == -1 && errno == EINVAL) { |
| 272 | LOG(INFO) << "sctp_status) not associated"; |
| 273 | return; |
| 274 | } |
| 275 | PCHECK(result == 0); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 276 | |
| 277 | LOG(INFO) << "sctp_status) sstat_assoc_id:" << status.sstat_assoc_id |
| 278 | << " sstat_state:" << status.sstat_state |
| 279 | << " sstat_rwnd:" << status.sstat_rwnd |
| 280 | << " sstat_unackdata:" << status.sstat_unackdata |
| 281 | << " sstat_penddata:" << status.sstat_penddata |
| 282 | << " sstat_instrms:" << status.sstat_instrms |
| 283 | << " sstat_outstrms:" << status.sstat_outstrms |
| 284 | << " sstat_fragmentation_point:" << status.sstat_fragmentation_point |
| 285 | << " sstat_primary.spinfo_srtt:" << status.sstat_primary.spinfo_srtt |
| 286 | << " sstat_primary.spinfo_rto:" << status.sstat_primary.spinfo_rto; |
| 287 | } |
| 288 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 289 | void SctpReadWrite::OpenSocket(const struct sockaddr_storage &sockaddr_local) { |
| 290 | fd_ = socket(sockaddr_local.ss_family, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 291 | PCHECK(fd_ != -1); |
| 292 | LOG(INFO) << "socket(" << Family(sockaddr_local) |
| 293 | << ", SOCK_SEQPACKET, IPPROTOSCTP) = " << fd_; |
| 294 | { |
Philipp Schrader | d2a5f5d | 2023-12-22 11:25:04 -0800 | [diff] [blame^] | 295 | // Set up Type-Of-Service. |
| 296 | // |
| 297 | // See comments for the --sctp_tos flag for more information. |
| 298 | int tos = IPTOS_DSCP(FLAGS_sctp_tos); |
| 299 | PCHECK(setsockopt(fd_, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == 0); |
| 300 | } |
| 301 | { |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 302 | // Per https://tools.ietf.org/html/rfc6458 |
| 303 | // Setting this to !0 allows event notifications to be interleaved |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 304 | // with data if enabled. This typically only matters during congestion. |
| 305 | // However, Linux seems to interleave under memory pressure regardless of |
| 306 | // this being enabled, so we have to handle it in the code anyways, so might |
| 307 | // as well turn it on all the time. |
| 308 | // TODO(Brian): Change this to 2 once we have kernels that support it, and |
| 309 | // also address the TODO in ProcessNotification to match on all the |
| 310 | // necessary fields. |
| 311 | int interleaving = 1; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 312 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE, |
| 313 | &interleaving, sizeof(interleaving)) == 0); |
| 314 | } |
| 315 | { |
| 316 | // Enable recvinfo when a packet arrives. |
| 317 | int on = 1; |
| 318 | PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) == |
| 319 | 0); |
| 320 | } |
| 321 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 322 | { |
| 323 | // TODO(austin): This is the old style registration... But, the sctp |
| 324 | // stack out in the wild for linux is old and primitive. |
| 325 | struct sctp_event_subscribe subscribe; |
| 326 | memset(&subscribe, 0, sizeof(subscribe)); |
| 327 | subscribe.sctp_association_event = 1; |
| 328 | subscribe.sctp_stream_change_event = 1; |
| 329 | subscribe.sctp_partial_delivery_event = 1; |
Adam Snaider | be26351 | 2023-05-18 20:40:23 -0700 | [diff] [blame] | 330 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_EVENTS, (char *)&subscribe, |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 331 | sizeof(subscribe)) == 0); |
| 332 | } |
| 333 | |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 334 | #if HAS_SCTP_AUTH |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 335 | if (sctp_authentication_) { |
| 336 | CHECK(SctpAuthIsEnabled()) |
| 337 | << "SCTP Authentication key requested, but authentication isn't " |
| 338 | "enabled... Use `sysctl -w net.sctp.auth_enable=1` to enable"; |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 339 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 340 | // Unfortunately there's no way to delete the null key if we don't have |
| 341 | // another key active so this is the only way to prevent unauthenticated |
| 342 | // traffic until the real shared key is established. |
| 343 | SetAuthKey(GenerateSecureRandomSequence(16)); |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 344 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 345 | // Disallow the null key. |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 346 | struct sctp_authkeyid authkeyid; |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 347 | authkeyid.scact_keynumber = 0; |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 348 | authkeyid.scact_assoc_id = SCTP_ALL_ASSOC; |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 349 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY, &authkeyid, |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 350 | sizeof(authkeyid)) == 0); |
| 351 | |
| 352 | // Set up authentication for data chunks. |
| 353 | struct sctp_authchunk authchunk; |
| 354 | authchunk.sauth_chunk = 0; |
| 355 | |
| 356 | PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_AUTH_CHUNK, &authchunk, |
| 357 | sizeof(authchunk)) == 0); |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 358 | } |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 359 | #endif |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 360 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 361 | DoSetMaxSize(); |
| 362 | } |
| 363 | |
| 364 | bool SctpReadWrite::SendMessage( |
| 365 | int stream, std::string_view data, int time_to_live, |
| 366 | std::optional<struct sockaddr_storage> sockaddr_remote, |
| 367 | sctp_assoc_t snd_assoc_id) { |
| 368 | CHECK(fd_ != -1); |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 369 | LOG_IF(FATAL, sctp_authentication_ && current_key_.empty()) |
| 370 | << "Expected SCTP authentication but no key active"; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 371 | struct iovec iov; |
| 372 | iov.iov_base = const_cast<char *>(data.data()); |
| 373 | iov.iov_len = data.size(); |
| 374 | |
| 375 | // Use the assoc_id for the destination instead of the msg_name. |
| 376 | struct msghdr outmsg; |
James Kuszmaul | 784deb7 | 2023-02-17 14:42:51 -0800 | [diff] [blame] | 377 | memset(&outmsg, 0, sizeof(outmsg)); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 378 | if (sockaddr_remote) { |
| 379 | outmsg.msg_name = &*sockaddr_remote; |
| 380 | outmsg.msg_namelen = sizeof(*sockaddr_remote); |
Sarah Newman | 4aeb237 | 2022-04-06 13:07:11 -0700 | [diff] [blame] | 381 | VLOG(2) << "Sending to " << Address(*sockaddr_remote); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 382 | } else { |
| 383 | outmsg.msg_namelen = 0; |
| 384 | } |
| 385 | |
| 386 | // Data to send. |
| 387 | outmsg.msg_iov = &iov; |
| 388 | outmsg.msg_iovlen = 1; |
| 389 | |
| 390 | // Build up the sndinfo message. |
| 391 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 392 | outmsg.msg_control = outcmsg; |
| 393 | outmsg.msg_controllen = sizeof(outcmsg); |
| 394 | outmsg.msg_flags = 0; |
| 395 | |
| 396 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg); |
| 397 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 398 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 399 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 400 | |
| 401 | struct sctp_sndrcvinfo *sinfo = |
| 402 | reinterpret_cast<struct sctp_sndrcvinfo *>(CMSG_DATA(cmsg)); |
| 403 | memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 404 | sinfo->sinfo_ppid = ++send_ppid_; |
| 405 | sinfo->sinfo_stream = stream; |
| 406 | sinfo->sinfo_flags = 0; |
| 407 | sinfo->sinfo_assoc_id = snd_assoc_id; |
| 408 | sinfo->sinfo_timetolive = time_to_live; |
| 409 | |
| 410 | // And send. |
| 411 | const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT); |
| 412 | if (size == -1) { |
| 413 | if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN || |
| 414 | errno == EINTR) { |
Austin Schuh | 581fab9 | 2022-02-07 19:50:54 -0800 | [diff] [blame] | 415 | if (VLOG_IS_ON(1)) { |
| 416 | PLOG(WARNING) << "sendmsg on sctp socket failed"; |
| 417 | } |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | PLOG(FATAL) << "sendmsg on sctp socket failed"; |
| 421 | return false; |
| 422 | } |
| 423 | CHECK_EQ(static_cast<ssize_t>(data.size()), size); |
Sarah Newman | 4aeb237 | 2022-04-06 13:07:11 -0700 | [diff] [blame] | 424 | VLOG(2) << "Sent " << data.size(); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 428 | void SctpReadWrite::FreeMessage(aos::unique_c_ptr<Message> &&message) { |
| 429 | if (use_pool_) { |
| 430 | free_messages_.emplace_back(std::move(message)); |
| 431 | } |
| 432 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 433 | |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 434 | void SctpReadWrite::SetPoolSize(size_t pool_size) { |
| 435 | CHECK(!use_pool_); |
| 436 | free_messages_.reserve(pool_size); |
| 437 | for (size_t i = 0; i < pool_size; ++i) { |
| 438 | free_messages_.emplace_back(AcquireMessage()); |
| 439 | } |
| 440 | use_pool_ = true; |
| 441 | } |
| 442 | |
| 443 | aos::unique_c_ptr<Message> SctpReadWrite::AcquireMessage() { |
| 444 | if (!use_pool_) { |
James Kuszmaul | 6e62238 | 2023-02-17 14:56:38 -0800 | [diff] [blame] | 445 | constexpr size_t kMessageAlign = alignof(Message); |
| 446 | const size_t max_message_size = |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 447 | ((sizeof(Message) + max_read_size_ + 1 + (kMessageAlign - 1)) / |
James Kuszmaul | 6e62238 | 2023-02-17 14:56:38 -0800 | [diff] [blame] | 448 | kMessageAlign) * |
| 449 | kMessageAlign; |
| 450 | aos::unique_c_ptr<Message> result(reinterpret_cast<Message *>( |
| 451 | aligned_alloc(kMessageAlign, max_message_size))); |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 452 | return result; |
| 453 | } else { |
| 454 | CHECK_GT(free_messages_.size(), 0u); |
| 455 | aos::unique_c_ptr<Message> result = std::move(free_messages_.back()); |
| 456 | free_messages_.pop_back(); |
| 457 | return result; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // We read each fragment into a fresh Message, because most of them won't be |
| 462 | // fragmented. If we do end up with a fragment, then we copy the data out of it. |
| 463 | aos::unique_c_ptr<Message> SctpReadWrite::ReadMessage() { |
| 464 | CHECK(fd_ != -1); |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 465 | LOG_IF(FATAL, sctp_authentication_ && current_key_.empty()) |
| 466 | << "Expected SCTP authentication but no key active"; |
Austin Schuh | f95a6ab | 2023-05-15 14:34:57 -0700 | [diff] [blame] | 467 | |
| 468 | while (true) { |
| 469 | aos::unique_c_ptr<Message> result = AcquireMessage(); |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 470 | |
Austin Schuh | 05c1812 | 2021-07-31 20:39:47 -0700 | [diff] [blame] | 471 | struct msghdr inmessage; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 472 | memset(&inmessage, 0, sizeof(struct msghdr)); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 473 | |
Austin Schuh | 05c1812 | 2021-07-31 20:39:47 -0700 | [diff] [blame] | 474 | struct iovec iov; |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 475 | iov.iov_len = max_read_size_ + 1; |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 476 | iov.iov_base = result->mutable_data(); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 477 | |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 478 | inmessage.msg_iov = &iov; |
| 479 | inmessage.msg_iovlen = 1; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 480 | |
Austin Schuh | 05c1812 | 2021-07-31 20:39:47 -0700 | [diff] [blame] | 481 | char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))]; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 482 | inmessage.msg_control = incmsg; |
| 483 | inmessage.msg_controllen = sizeof(incmsg); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 484 | |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 485 | inmessage.msg_namelen = sizeof(struct sockaddr_storage); |
| 486 | inmessage.msg_name = &result->sin; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 487 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 488 | const ssize_t size = recvmsg(fd_, &inmessage, MSG_DONTWAIT); |
| 489 | if (size == -1) { |
| 490 | if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { |
| 491 | // These are all non-fatal failures indicating we should retry later. |
| 492 | return nullptr; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 493 | } |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 494 | PLOG(FATAL) << "recvmsg on sctp socket " << fd_ << " failed"; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 497 | CHECK(!(inmessage.msg_flags & MSG_CTRUNC)) |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 498 | << ": Control message truncated."; |
| 499 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 500 | if (MSG_NOTIFICATION & inmessage.msg_flags) { |
| 501 | result->message_type = Message::kNotification; |
| 502 | } else { |
| 503 | result->message_type = Message::kMessage; |
| 504 | } |
| 505 | result->partial_deliveries = 0; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 506 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 507 | { |
| 508 | bool found_rcvinfo = false; |
| 509 | for (struct cmsghdr *scmsg = CMSG_FIRSTHDR(&inmessage); scmsg != NULL; |
| 510 | scmsg = CMSG_NXTHDR(&inmessage, scmsg)) { |
| 511 | switch (scmsg->cmsg_type) { |
| 512 | case SCTP_RCVINFO: { |
| 513 | CHECK(!found_rcvinfo); |
| 514 | found_rcvinfo = true; |
| 515 | result->header.rcvinfo = |
| 516 | *reinterpret_cast<struct sctp_rcvinfo *>(CMSG_DATA(scmsg)); |
| 517 | } break; |
| 518 | default: |
| 519 | LOG(INFO) << "\tUnknown type: " << scmsg->cmsg_type; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | CHECK_EQ(found_rcvinfo, result->message_type == Message::kMessage) |
| 524 | << ": Failed to find a SCTP_RCVINFO cmsghdr. flags: " |
| 525 | << inmessage.msg_flags; |
| 526 | } |
Austin Schuh | 89f23e3 | 2023-05-15 17:06:43 -0700 | [diff] [blame] | 527 | |
| 528 | // Client just sent too big a block of data. Eat it and signal up the |
| 529 | // chain. |
| 530 | result->size = size; |
| 531 | if (size > static_cast<ssize_t>(max_read_size_)) { |
| 532 | Abort(result->header.rcvinfo.rcv_assoc_id); |
| 533 | result->message_type = Message::kOverflow; |
| 534 | |
| 535 | VLOG(1) << "Message overflowed buffer on stream " |
| 536 | << result->header.rcvinfo.rcv_sid << ", disconnecting." |
| 537 | << " Check for config mismatch or rogue device."; |
| 538 | return result; |
| 539 | } |
| 540 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 541 | if (result->message_type == Message::kNotification) { |
| 542 | // Notifications are never fragmented, just return it now. |
| 543 | CHECK(inmessage.msg_flags & MSG_EOR) |
| 544 | << ": Notifications should never be big enough to fragment"; |
| 545 | if (ProcessNotification(result.get())) { |
| 546 | // We handled this notification internally, so don't pass it on. |
| 547 | return nullptr; |
| 548 | } |
| 549 | return result; |
| 550 | } |
| 551 | |
| 552 | auto partial_message_iterator = |
| 553 | std::find_if(partial_messages_.begin(), partial_messages_.end(), |
| 554 | [&result](const aos::unique_c_ptr<Message> &candidate) { |
| 555 | return result->header.rcvinfo.rcv_sid == |
| 556 | candidate->header.rcvinfo.rcv_sid && |
| 557 | result->header.rcvinfo.rcv_ssn == |
| 558 | candidate->header.rcvinfo.rcv_ssn && |
| 559 | result->header.rcvinfo.rcv_assoc_id == |
| 560 | candidate->header.rcvinfo.rcv_assoc_id; |
| 561 | }); |
| 562 | if (partial_message_iterator != partial_messages_.end()) { |
| 563 | const aos::unique_c_ptr<Message> &partial_message = |
| 564 | *partial_message_iterator; |
| 565 | // Verify it's really part of the same message. |
| 566 | CHECK_EQ(partial_message->message_type, result->message_type) |
| 567 | << ": for " << result->header.rcvinfo.rcv_sid << "," |
| 568 | << result->header.rcvinfo.rcv_ssn << "," |
| 569 | << result->header.rcvinfo.rcv_assoc_id; |
| 570 | CHECK_EQ(partial_message->header.rcvinfo.rcv_ppid, |
| 571 | result->header.rcvinfo.rcv_ppid) |
| 572 | << ": for " << result->header.rcvinfo.rcv_sid << "," |
| 573 | << result->header.rcvinfo.rcv_ssn << "," |
| 574 | << result->header.rcvinfo.rcv_assoc_id; |
| 575 | |
| 576 | // Now copy the data over and update the size. |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 577 | CHECK_LE(partial_message->size + result->size, max_read_size_) |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 578 | << ": Assembled fragments overflowed buffer on stream " |
| 579 | << result->header.rcvinfo.rcv_sid << "."; |
| 580 | memcpy(partial_message->mutable_data() + partial_message->size, |
| 581 | result->data(), result->size); |
| 582 | ++partial_message->partial_deliveries; |
Sarah Newman | 4aeb237 | 2022-04-06 13:07:11 -0700 | [diff] [blame] | 583 | VLOG(2) << "Merged fragment of " << result->size << " after " |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 584 | << partial_message->size << ", had " |
| 585 | << partial_message->partial_deliveries |
| 586 | << ", for: " << result->header.rcvinfo.rcv_sid << "," |
| 587 | << result->header.rcvinfo.rcv_ssn << "," |
| 588 | << result->header.rcvinfo.rcv_assoc_id; |
| 589 | partial_message->size += result->size; |
| 590 | result.reset(); |
| 591 | } |
| 592 | |
| 593 | if (inmessage.msg_flags & MSG_EOR) { |
| 594 | // This is the last fragment, so we have something to return. |
| 595 | if (partial_message_iterator != partial_messages_.end()) { |
| 596 | // It was already merged into the message in the list, so now we pull |
| 597 | // that out of the list and return it. |
| 598 | CHECK(!result); |
| 599 | result = std::move(*partial_message_iterator); |
| 600 | partial_messages_.erase(partial_message_iterator); |
| 601 | VLOG(1) << "Final count: " << (result->partial_deliveries + 1) |
| 602 | << ", size: " << result->size |
| 603 | << ", for: " << result->header.rcvinfo.rcv_sid << "," |
| 604 | << result->header.rcvinfo.rcv_ssn << "," |
| 605 | << result->header.rcvinfo.rcv_assoc_id; |
| 606 | } |
| 607 | CHECK(result); |
| 608 | return result; |
| 609 | } |
| 610 | if (partial_message_iterator == partial_messages_.end()) { |
Sarah Newman | 4aeb237 | 2022-04-06 13:07:11 -0700 | [diff] [blame] | 611 | VLOG(2) << "Starting fragment for: " << result->header.rcvinfo.rcv_sid |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 612 | << "," << result->header.rcvinfo.rcv_ssn << "," |
| 613 | << result->header.rcvinfo.rcv_assoc_id; |
| 614 | // Need to record this as the first fragment. |
| 615 | partial_messages_.emplace_back(std::move(result)); |
| 616 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 617 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 620 | bool SctpReadWrite::Abort(sctp_assoc_t snd_assoc_id) { |
| 621 | if (fd_ == -1) { |
| 622 | return true; |
| 623 | } |
| 624 | VLOG(1) << "Sending abort to assoc " << snd_assoc_id; |
| 625 | |
| 626 | // Use the assoc_id for the destination instead of the msg_name. |
| 627 | struct msghdr outmsg; |
James Kuszmaul | 784deb7 | 2023-02-17 14:42:51 -0800 | [diff] [blame] | 628 | memset(&outmsg, 0, sizeof(outmsg)); |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 629 | outmsg.msg_namelen = 0; |
| 630 | |
| 631 | outmsg.msg_iovlen = 0; |
| 632 | |
| 633 | // Build up the sndinfo message. |
| 634 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 635 | outmsg.msg_control = outcmsg; |
| 636 | outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); |
| 637 | outmsg.msg_flags = 0; |
| 638 | |
| 639 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg); |
| 640 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 641 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 642 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 643 | |
| 644 | struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 645 | memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 646 | sinfo->sinfo_stream = 0; |
| 647 | sinfo->sinfo_flags = SCTP_ABORT; |
| 648 | sinfo->sinfo_assoc_id = snd_assoc_id; |
| 649 | |
| 650 | // And send. |
| 651 | const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT); |
| 652 | if (size == -1) { |
| 653 | if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) { |
| 654 | return false; |
| 655 | } |
| 656 | return false; |
| 657 | } else { |
| 658 | CHECK_EQ(0, size); |
| 659 | return true; |
| 660 | } |
| 661 | } |
| 662 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 663 | void SctpReadWrite::CloseSocket() { |
| 664 | if (fd_ == -1) { |
| 665 | return; |
| 666 | } |
| 667 | LOG(INFO) << "close(" << fd_ << ")"; |
| 668 | PCHECK(close(fd_) == 0); |
| 669 | fd_ = -1; |
| 670 | } |
| 671 | |
| 672 | void SctpReadWrite::DoSetMaxSize() { |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 673 | size_t max_size = max_write_size_; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 674 | |
Austin Schuh | 6122488 | 2021-10-11 18:21:11 -0700 | [diff] [blame] | 675 | // This sets the max packet size that we can send. |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 676 | CHECK_GE(ReadWMemMax(), max_write_size_) |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 677 | << "wmem_max is too low. To increase wmem_max temporarily, do sysctl " |
| 678 | "-w net.core.wmem_max=" |
| 679 | << max_size; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 680 | PCHECK(setsockopt(fd(), SOL_SOCKET, SO_SNDBUF, &max_size, sizeof(max_size)) == |
| 681 | 0); |
Austin Schuh | 6122488 | 2021-10-11 18:21:11 -0700 | [diff] [blame] | 682 | |
| 683 | // The SO_RCVBUF option (also controlled by net.core.rmem_default) needs to be |
| 684 | // decently large but the actual size can be measured by tuning. The defaults |
| 685 | // should be fine. If it isn't big enough, transmission will fail. |
Austin Schuh | 9dd8f59 | 2021-12-25 14:32:43 -0800 | [diff] [blame] | 686 | if (FLAGS_rmem > 0) { |
| 687 | size_t rmem = FLAGS_rmem; |
| 688 | PCHECK(setsockopt(fd(), SOL_SOCKET, SO_RCVBUF, &rmem, sizeof(rmem)) == 0); |
| 689 | } |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 692 | bool SctpReadWrite::ProcessNotification(const Message *message) { |
| 693 | const union sctp_notification *const snp = |
| 694 | reinterpret_cast<const union sctp_notification *>(message->data()); |
| 695 | switch (snp->sn_header.sn_type) { |
| 696 | case SCTP_PARTIAL_DELIVERY_EVENT: { |
| 697 | const struct sctp_pdapi_event *const partial_delivery = |
| 698 | &snp->sn_pdapi_event; |
| 699 | CHECK_EQ(partial_delivery->pdapi_length, sizeof(*partial_delivery)) |
| 700 | << ": Kernel's SCTP code is not a version we support"; |
| 701 | switch (partial_delivery->pdapi_indication) { |
| 702 | case SCTP_PARTIAL_DELIVERY_ABORTED: { |
| 703 | const auto iterator = std::find_if( |
| 704 | partial_messages_.begin(), partial_messages_.end(), |
| 705 | [partial_delivery](const aos::unique_c_ptr<Message> &candidate) { |
| 706 | // TODO(Brian): Once we have new enough userpace headers, for |
| 707 | // kernels that support level-2 interleaving, we'll need to add |
| 708 | // this: |
| 709 | // candidate->header.rcvinfo.rcv_sid == |
| 710 | // partial_delivery->pdapi_stream && |
| 711 | // candidate->header.rcvinfo.rcv_ssn == |
| 712 | // partial_delivery->pdapi_seq && |
| 713 | return candidate->header.rcvinfo.rcv_assoc_id == |
| 714 | partial_delivery->pdapi_assoc_id; |
| 715 | }); |
| 716 | CHECK(iterator != partial_messages_.end()) |
| 717 | << ": Got out of sync with the kernel for " |
| 718 | << partial_delivery->pdapi_assoc_id; |
| 719 | VLOG(1) << "Pruning partial delivery for " |
| 720 | << iterator->get()->header.rcvinfo.rcv_sid << "," |
| 721 | << iterator->get()->header.rcvinfo.rcv_ssn << "," |
| 722 | << iterator->get()->header.rcvinfo.rcv_assoc_id; |
| 723 | partial_messages_.erase(iterator); |
| 724 | } |
| 725 | return true; |
| 726 | } |
| 727 | } break; |
| 728 | } |
| 729 | return false; |
| 730 | } |
| 731 | |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 732 | void SctpReadWrite::SetAuthKey(absl::Span<const uint8_t> auth_key) { |
| 733 | PCHECK(fd_ != -1); |
| 734 | if (auth_key.empty()) { |
| 735 | return; |
| 736 | } |
| 737 | // We are already using the key, nothing to do. |
| 738 | if (auth_key == current_key_) { |
| 739 | return; |
| 740 | } |
| 741 | #if !(HAS_SCTP_AUTH) |
| 742 | LOG(FATAL) << "SCTP Authentication key requested, but authentication isn't " |
| 743 | "available... You may need a newer kernel"; |
| 744 | #else |
| 745 | LOG_IF(FATAL, !SctpAuthIsEnabled()) |
| 746 | << "SCTP Authentication key requested, but authentication isn't " |
| 747 | "enabled... Use `sysctl -w net.sctp.auth_enable=1` to enable"; |
| 748 | // Set up the key with id `1`. |
Adam Snaider | 82d1dc7 | 2023-09-26 09:13:55 -0700 | [diff] [blame] | 749 | // NOTE: `sctp_authkey` is a variable-sized struct which is why it needs |
| 750 | // to be heap allocated. Regardless, this object doesn't have to persist past |
| 751 | // the `setsockopt` call below. |
| 752 | std::unique_ptr<sctp_authkey> authkey(static_cast<sctp_authkey *>( |
| 753 | ::operator new(sizeof(sctp_authkey) + auth_key.size()))); |
Adam Snaider | 9bb3344 | 2023-06-26 16:31:37 -0700 | [diff] [blame] | 754 | |
| 755 | authkey->sca_keynumber = 1; |
| 756 | authkey->sca_keylength = auth_key.size(); |
| 757 | authkey->sca_assoc_id = SCTP_ALL_ASSOC; |
| 758 | memcpy(&authkey->sca_key, auth_key.data(), auth_key.size()); |
| 759 | |
| 760 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_AUTH_KEY, authkey.get(), |
| 761 | sizeof(sctp_authkey) + auth_key.size()) != 0) { |
| 762 | if (errno == EACCES) { |
| 763 | // TODO(adam.snaider): Figure out why this fails when expected nodes are |
| 764 | // not connected. |
| 765 | PLOG_EVERY_N(ERROR, 100) << "Setting authentication key failed"; |
| 766 | return; |
| 767 | } else { |
| 768 | PLOG(FATAL) << "Setting authentication key failed"; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // Set key `1` as active. |
| 773 | struct sctp_authkeyid authkeyid; |
| 774 | authkeyid.scact_keynumber = 1; |
| 775 | authkeyid.scact_assoc_id = SCTP_ALL_ASSOC; |
| 776 | if (setsockopt(fd(), IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid, |
| 777 | sizeof(authkeyid)) != 0) { |
| 778 | PLOG(FATAL) << "Setting key id `1` as active failed"; |
| 779 | } |
| 780 | current_key_.assign(auth_key.begin(), auth_key.end()); |
| 781 | #endif |
| 782 | } // namespace message_bridge |
| 783 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 784 | void Message::LogRcvInfo() const { |
| 785 | LOG(INFO) << "\tSNDRCV (stream=" << header.rcvinfo.rcv_sid |
| 786 | << " ssn=" << header.rcvinfo.rcv_ssn |
| 787 | << " tsn=" << header.rcvinfo.rcv_tsn << " flags=0x" << std::hex |
| 788 | << header.rcvinfo.rcv_flags << std::dec |
| 789 | << " ppid=" << header.rcvinfo.rcv_ppid |
| 790 | << " cumtsn=" << header.rcvinfo.rcv_cumtsn << ")"; |
| 791 | } |
| 792 | |
Austin Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 793 | size_t ReadRMemMax() { |
| 794 | struct stat current_stat; |
| 795 | if (stat("/proc/sys/net/core/rmem_max", ¤t_stat) != -1) { |
| 796 | return static_cast<size_t>( |
| 797 | std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/rmem_max"))); |
| 798 | } else { |
| 799 | LOG(WARNING) << "/proc/sys/net/core/rmem_max doesn't exist. Are you in a " |
| 800 | "container?"; |
| 801 | return 212992; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | size_t ReadWMemMax() { |
| 806 | struct stat current_stat; |
| 807 | if (stat("/proc/sys/net/core/wmem_max", ¤t_stat) != -1) { |
| 808 | return static_cast<size_t>( |
| 809 | std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/wmem_max"))); |
| 810 | } else { |
| 811 | LOG(WARNING) << "/proc/sys/net/core/wmem_max doesn't exist. Are you in a " |
| 812 | "container?"; |
| 813 | return 212992; |
| 814 | } |
| 815 | } |
| 816 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 817 | } // namespace aos::message_bridge |