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