blob: cf50ad66dd4b8b7611b8761a8e3332a9ed0bd7da [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#include "aos/network/sctp_lib.h"
2
3#include <arpa/inet.h>
Adam Snaiderbe263512023-05-18 20:40:23 -07004#include <linux/sctp.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08005#include <net/if.h>
6#include <netdb.h>
Philipp Schraderd2a5f5d2023-12-22 11:25:04 -08007#include <netinet/ip.h>
Adam Snaiderbe263512023-05-18 20:40:23 -07008#include <sys/socket.h>
Austin Schuh2fe4b712020-03-15 14:21:45 -07009#include <sys/stat.h>
10#include <sys/types.h>
11#include <unistd.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080012
Austin Schuha705d782021-07-31 20:40:00 -070013#include <algorithm>
Adam Snaider9bb33442023-06-26 16:31:37 -070014#include <cerrno>
15#include <fstream>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080016#include <string_view>
Adam Snaider9bb33442023-06-26 16:31:37 -070017#include <vector>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080018
Austin Schuh2fe4b712020-03-15 14:21:45 -070019#include "aos/util/file.h"
20
Adam Snaider13d48d92023-08-03 12:20:15 -070021// 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 Schuh0a0a8272021-12-08 13:19:32 -080026DEFINE_string(interface, "", "network interface");
Brian Silverman0c6d44e2021-11-10 12:27:49 -080027DEFINE_bool(disable_ipv6, false, "disable ipv6");
Austin Schuh9dd8f592021-12-25 14:32:43 -080028DEFINE_int32(rmem, 0, "If nonzero, set rmem to this size.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080029
Philipp Schraderd2a5f5d2023-12-22 11:25:04 -080030// 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.
42DEFINE_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 Pleinesf63bde82024-01-13 15:59:33 -080049namespace aos::message_bridge {
Austin Schuhe84c3ed2019-12-14 15:29:48 -080050
51namespace {
52const char *sac_state_tbl[] = {"COMMUNICATION_UP", "COMMUNICATION_LOST",
53 "RESTART", "SHUTDOWN_COMPLETE",
Sarah Newman4aeb2372022-04-06 13:07:11 -070054 "CANT_START_ASSOCIATION"};
Austin Schuhe84c3ed2019-12-14 15:29:48 -080055
56typedef union {
57 struct sctp_initmsg init;
58 struct sctp_sndrcvinfo sndrcvinfo;
59} _sctp_cmsg_data_t;
60
Adam Snaider9bb33442023-06-26 16:31:37 -070061#if HAS_SCTP_AUTH
Adam Snaider96a0f4b2023-05-18 20:41:19 -070062// Returns true if SCTP authentication is available and enabled.
63bool SctpAuthIsEnabled() {
Adam Snaider96a0f4b2023-05-18 20:41:19 -070064 struct stat current_stat;
65 if (stat("/proc/sys/net/sctp/auth_enable", &current_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 Snaider96a0f4b2023-05-18 20:41:19 -070075}
76
Adam Snaider9bb33442023-06-26 16:31:37 -070077std::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 Schuhe84c3ed2019-12-14 15:29:48 -080088} // namespace
89
Austin Schuh0a0a8272021-12-08 13:19:32 -080090bool 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
111struct sockaddr_storage ResolveSocket(std::string_view host, int port,
112 bool use_ipv6) {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800113 struct sockaddr_storage result;
James Kuszmaul784deb72023-02-17 14:42:51 -0800114 memset(&result, 0, sizeof(result));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800115 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 Silverman0c6d44e2021-11-10 12:27:49 -0800118 struct addrinfo hints;
119 memset(&hints, 0, sizeof(hints));
Austin Schuh0a0a8272021-12-08 13:19:32 -0800120 if (!use_ipv6) {
Brian Silverman0c6d44e2021-11-10 12:27:49 -0800121 hints.ai_family = AF_INET;
122 } else {
Austin Schuh0a0a8272021-12-08 13:19:32 -0800123 // Default to IPv6 as the clearly superior protocol, since it also handles
124 // IPv4.
Brian Silverman0c6d44e2021-11-10 12:27:49 -0800125 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 Silverman0c6d44e2021-11-10 12:27:49 -0800136 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 Schuhe84c3ed2019-12-14 15:29:48 -0800142 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
181std::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}
190std::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
203void 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 Schuhf7777002020-09-01 18:41:28 -0700237 case SCTP_STREAM_CHANGE_EVENT: {
Austin Schuh62a0c272021-03-31 21:04:53 -0700238 const struct sctp_stream_change_event *sce = &snp->sn_strchange_event;
Austin Schuhf7777002020-09-01 18:41:28 -0700239 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 Schuhe84c3ed2019-12-14 15:29:48 -0800245 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
254std::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
261std::string Message::PeerAddress() const { return Address(sin); }
262
263void 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 Snaiderbe263512023-05-18 20:40:23 -0700269 const int result = getsockopt(fd, IPPROTO_SCTP, SCTP_STATUS,
Austin Schuha5f545b2021-07-31 20:39:42 -0700270 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 Schuhe84c3ed2019-12-14 15:29:48 -0800276
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 Schuh507f7582021-07-31 20:39:55 -0700289void 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 Schraderd2a5f5d2023-12-22 11:25:04 -0800295 // 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 Schuh507f7582021-07-31 20:39:55 -0700302 // Per https://tools.ietf.org/html/rfc6458
303 // Setting this to !0 allows event notifications to be interleaved
Austin Schuha705d782021-07-31 20:40:00 -0700304 // 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 Schuh507f7582021-07-31 20:39:55 -0700312 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 Schuha705d782021-07-31 20:40:00 -0700322 {
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 Snaiderbe263512023-05-18 20:40:23 -0700330 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_EVENTS, (char *)&subscribe,
Austin Schuha705d782021-07-31 20:40:00 -0700331 sizeof(subscribe)) == 0);
332 }
333
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700334#if HAS_SCTP_AUTH
Adam Snaider9bb33442023-06-26 16:31:37 -0700335 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 Snaider96a0f4b2023-05-18 20:41:19 -0700339
Adam Snaider9bb33442023-06-26 16:31:37 -0700340 // 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 Snaider96a0f4b2023-05-18 20:41:19 -0700344
Adam Snaider9bb33442023-06-26 16:31:37 -0700345 // Disallow the null key.
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700346 struct sctp_authkeyid authkeyid;
Adam Snaider9bb33442023-06-26 16:31:37 -0700347 authkeyid.scact_keynumber = 0;
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700348 authkeyid.scact_assoc_id = SCTP_ALL_ASSOC;
Adam Snaider9bb33442023-06-26 16:31:37 -0700349 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY, &authkeyid,
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700350 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 Snaider96a0f4b2023-05-18 20:41:19 -0700358 }
Adam Snaider9bb33442023-06-26 16:31:37 -0700359#endif
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700360
Austin Schuh507f7582021-07-31 20:39:55 -0700361 DoSetMaxSize();
362}
363
364bool 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 Snaider9bb33442023-06-26 16:31:37 -0700369 LOG_IF(FATAL, sctp_authentication_ && current_key_.empty())
370 << "Expected SCTP authentication but no key active";
Austin Schuh507f7582021-07-31 20:39:55 -0700371 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 Kuszmaul784deb72023-02-17 14:42:51 -0800377 memset(&outmsg, 0, sizeof(outmsg));
Austin Schuh507f7582021-07-31 20:39:55 -0700378 if (sockaddr_remote) {
379 outmsg.msg_name = &*sockaddr_remote;
380 outmsg.msg_namelen = sizeof(*sockaddr_remote);
Sarah Newman4aeb2372022-04-06 13:07:11 -0700381 VLOG(2) << "Sending to " << Address(*sockaddr_remote);
Austin Schuh507f7582021-07-31 20:39:55 -0700382 } 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 Schuh581fab92022-02-07 19:50:54 -0800415 if (VLOG_IS_ON(1)) {
416 PLOG(WARNING) << "sendmsg on sctp socket failed";
417 }
Austin Schuh507f7582021-07-31 20:39:55 -0700418 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 Newman4aeb2372022-04-06 13:07:11 -0700424 VLOG(2) << "Sent " << data.size();
Austin Schuh507f7582021-07-31 20:39:55 -0700425 return true;
426}
427
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700428void SctpReadWrite::FreeMessage(aos::unique_c_ptr<Message> &&message) {
429 if (use_pool_) {
430 free_messages_.emplace_back(std::move(message));
431 }
432}
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800433
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700434void 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
443aos::unique_c_ptr<Message> SctpReadWrite::AcquireMessage() {
444 if (!use_pool_) {
James Kuszmaul6e622382023-02-17 14:56:38 -0800445 constexpr size_t kMessageAlign = alignof(Message);
446 const size_t max_message_size =
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700447 ((sizeof(Message) + max_read_size_ + 1 + (kMessageAlign - 1)) /
James Kuszmaul6e622382023-02-17 14:56:38 -0800448 kMessageAlign) *
449 kMessageAlign;
450 aos::unique_c_ptr<Message> result(reinterpret_cast<Message *>(
451 aligned_alloc(kMessageAlign, max_message_size)));
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700452 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.
463aos::unique_c_ptr<Message> SctpReadWrite::ReadMessage() {
464 CHECK(fd_ != -1);
Adam Snaider9bb33442023-06-26 16:31:37 -0700465 LOG_IF(FATAL, sctp_authentication_ && current_key_.empty())
466 << "Expected SCTP authentication but no key active";
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700467
468 while (true) {
469 aos::unique_c_ptr<Message> result = AcquireMessage();
Austin Schuha705d782021-07-31 20:40:00 -0700470
Austin Schuh05c18122021-07-31 20:39:47 -0700471 struct msghdr inmessage;
Austin Schuhc4202572021-03-31 21:06:55 -0700472 memset(&inmessage, 0, sizeof(struct msghdr));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800473
Austin Schuh05c18122021-07-31 20:39:47 -0700474 struct iovec iov;
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700475 iov.iov_len = max_read_size_ + 1;
Austin Schuha705d782021-07-31 20:40:00 -0700476 iov.iov_base = result->mutable_data();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800477
Austin Schuhc4202572021-03-31 21:06:55 -0700478 inmessage.msg_iov = &iov;
479 inmessage.msg_iovlen = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800480
Austin Schuh05c18122021-07-31 20:39:47 -0700481 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
Austin Schuhc4202572021-03-31 21:06:55 -0700482 inmessage.msg_control = incmsg;
483 inmessage.msg_controllen = sizeof(incmsg);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800484
Austin Schuhc4202572021-03-31 21:06:55 -0700485 inmessage.msg_namelen = sizeof(struct sockaddr_storage);
486 inmessage.msg_name = &result->sin;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800487
Austin Schuha705d782021-07-31 20:40:00 -0700488 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 Schuhc4202572021-03-31 21:06:55 -0700493 }
Austin Schuha705d782021-07-31 20:40:00 -0700494 PLOG(FATAL) << "recvmsg on sctp socket " << fd_ << " failed";
Austin Schuhc4202572021-03-31 21:06:55 -0700495 }
496
Austin Schuha705d782021-07-31 20:40:00 -0700497 CHECK(!(inmessage.msg_flags & MSG_CTRUNC))
Austin Schuhc4202572021-03-31 21:06:55 -0700498 << ": Control message truncated.";
499
Austin Schuha705d782021-07-31 20:40:00 -0700500 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 Schuhc4202572021-03-31 21:06:55 -0700506
Austin Schuha705d782021-07-31 20:40:00 -0700507 {
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 Schuh89f23e32023-05-15 17:06:43 -0700527
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 Schuha705d782021-07-31 20:40:00 -0700541 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 Schuh89e1e9c2023-05-15 14:38:44 -0700577 CHECK_LE(partial_message->size + result->size, max_read_size_)
Austin Schuha705d782021-07-31 20:40:00 -0700578 << ": 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 Newman4aeb2372022-04-06 13:07:11 -0700583 VLOG(2) << "Merged fragment of " << result->size << " after "
Austin Schuha705d782021-07-31 20:40:00 -0700584 << 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 Newman4aeb2372022-04-06 13:07:11 -0700611 VLOG(2) << "Starting fragment for: " << result->header.rcvinfo.rcv_sid
Austin Schuha705d782021-07-31 20:40:00 -0700612 << "," << 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 Schuhe84c3ed2019-12-14 15:29:48 -0800617 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800618}
619
Sarah Newman80e955e2022-04-13 11:19:36 -0700620bool 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 Kuszmaul784deb72023-02-17 14:42:51 -0800628 memset(&outmsg, 0, sizeof(outmsg));
Sarah Newman80e955e2022-04-13 11:19:36 -0700629 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 Schuh507f7582021-07-31 20:39:55 -0700663void SctpReadWrite::CloseSocket() {
664 if (fd_ == -1) {
665 return;
666 }
667 LOG(INFO) << "close(" << fd_ << ")";
668 PCHECK(close(fd_) == 0);
669 fd_ = -1;
670}
671
672void SctpReadWrite::DoSetMaxSize() {
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700673 size_t max_size = max_write_size_;
Austin Schuh507f7582021-07-31 20:39:55 -0700674
Austin Schuh61224882021-10-11 18:21:11 -0700675 // This sets the max packet size that we can send.
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700676 CHECK_GE(ReadWMemMax(), max_write_size_)
Austin Schuh507f7582021-07-31 20:39:55 -0700677 << "wmem_max is too low. To increase wmem_max temporarily, do sysctl "
678 "-w net.core.wmem_max="
679 << max_size;
Austin Schuh507f7582021-07-31 20:39:55 -0700680 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_SNDBUF, &max_size, sizeof(max_size)) ==
681 0);
Austin Schuh61224882021-10-11 18:21:11 -0700682
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 Schuh9dd8f592021-12-25 14:32:43 -0800686 if (FLAGS_rmem > 0) {
687 size_t rmem = FLAGS_rmem;
688 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_RCVBUF, &rmem, sizeof(rmem)) == 0);
689 }
Austin Schuh507f7582021-07-31 20:39:55 -0700690}
691
Austin Schuha705d782021-07-31 20:40:00 -0700692bool 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 Snaider9bb33442023-06-26 16:31:37 -0700732void 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 Snaider82d1dc72023-09-26 09:13:55 -0700749 // 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 Snaider9bb33442023-06-26 16:31:37 -0700754
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 Schuhe84c3ed2019-12-14 15:29:48 -0800784void 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 Schuh2fe4b712020-03-15 14:21:45 -0700793size_t ReadRMemMax() {
794 struct stat current_stat;
795 if (stat("/proc/sys/net/core/rmem_max", &current_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
805size_t ReadWMemMax() {
806 struct stat current_stat;
807 if (stat("/proc/sys/net/core/wmem_max", &current_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 Pleinesf63bde82024-01-13 15:59:33 -0800817} // namespace aos::message_bridge