blob: 1c55293aa262158b4f8bef0e7bc0ae578bc73fa0 [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 Schuh99f7c6a2024-06-25 22:07:44 -070019#include "absl/flags/flag.h"
20#include "absl/log/check.h"
21#include "absl/log/log.h"
22
Austin Schuh2fe4b712020-03-15 14:21:45 -070023#include "aos/util/file.h"
24
Adam Snaider13d48d92023-08-03 12:20:15 -070025// 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 Schuh99f7c6a2024-06-25 22:07:44 -070030ABSL_FLAG(std::string, interface, "", "network interface");
31ABSL_FLAG(bool, disable_ipv6, false, "disable ipv6");
32ABSL_FLAG(int32_t, rmem, 0, "If nonzero, set rmem to this size.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080033
Philipp Schraderd2a5f5d2023-12-22 11:25:04 -080034// 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 Schuh99f7c6a2024-06-25 22:07:44 -070046ABSL_FLAG(
47 int32_t, sctp_tos, 176,
Philipp Schraderd2a5f5d2023-12-22 11:25:04 -080048 "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 Pleinesf63bde82024-01-13 15:59:33 -080053namespace aos::message_bridge {
Austin Schuhe84c3ed2019-12-14 15:29:48 -080054
55namespace {
56const char *sac_state_tbl[] = {"COMMUNICATION_UP", "COMMUNICATION_LOST",
57 "RESTART", "SHUTDOWN_COMPLETE",
Sarah Newman4aeb2372022-04-06 13:07:11 -070058 "CANT_START_ASSOCIATION"};
Austin Schuhe84c3ed2019-12-14 15:29:48 -080059
60typedef union {
61 struct sctp_initmsg init;
62 struct sctp_sndrcvinfo sndrcvinfo;
63} _sctp_cmsg_data_t;
64
Adam Snaider9bb33442023-06-26 16:31:37 -070065#if HAS_SCTP_AUTH
Adam Snaider96a0f4b2023-05-18 20:41:19 -070066// Returns true if SCTP authentication is available and enabled.
67bool SctpAuthIsEnabled() {
Adam Snaider96a0f4b2023-05-18 20:41:19 -070068 struct stat current_stat;
69 if (stat("/proc/sys/net/sctp/auth_enable", &current_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 Snaider96a0f4b2023-05-18 20:41:19 -070079}
80
Adam Snaider9bb33442023-06-26 16:31:37 -070081std::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 Schuhe84c3ed2019-12-14 15:29:48 -080092} // namespace
93
Austin Schuh0a0a8272021-12-08 13:19:32 -080094bool Ipv6Enabled() {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070095 if (absl::GetFlag(FLAGS_disable_ipv6)) {
Austin Schuh0a0a8272021-12-08 13:19:32 -080096 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
115struct sockaddr_storage ResolveSocket(std::string_view host, int port,
116 bool use_ipv6) {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800117 struct sockaddr_storage result;
James Kuszmaul784deb72023-02-17 14:42:51 -0800118 memset(&result, 0, sizeof(result));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800119 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 Silverman0c6d44e2021-11-10 12:27:49 -0800122 struct addrinfo hints;
123 memset(&hints, 0, sizeof(hints));
Austin Schuh0a0a8272021-12-08 13:19:32 -0800124 if (!use_ipv6) {
Brian Silverman0c6d44e2021-11-10 12:27:49 -0800125 hints.ai_family = AF_INET;
126 } else {
Austin Schuh0a0a8272021-12-08 13:19:32 -0800127 // Default to IPv6 as the clearly superior protocol, since it also handles
128 // IPv4.
Brian Silverman0c6d44e2021-11-10 12:27:49 -0800129 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 Silverman0c6d44e2021-11-10 12:27:49 -0800140 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 Schuhe84c3ed2019-12-14 15:29:48 -0800146 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 Schuh99f7c6a2024-06-25 22:07:44 -0700158 if (absl::GetFlag(FLAGS_interface).size() > 0) {
159 t_addr6->sin6_scope_id =
160 if_nametoindex(absl::GetFlag(FLAGS_interface).c_str());
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800161 }
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
186std::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}
195std::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
208void 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 Schuhf7777002020-09-01 18:41:28 -0700242 case SCTP_STREAM_CHANGE_EVENT: {
Austin Schuh62a0c272021-03-31 21:04:53 -0700243 const struct sctp_stream_change_event *sce = &snp->sn_strchange_event;
Austin Schuhf7777002020-09-01 18:41:28 -0700244 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 Schuhe84c3ed2019-12-14 15:29:48 -0800250 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
259std::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
266std::string Message::PeerAddress() const { return Address(sin); }
267
268void 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 Snaiderbe263512023-05-18 20:40:23 -0700274 const int result = getsockopt(fd, IPPROTO_SCTP, SCTP_STATUS,
Austin Schuha5f545b2021-07-31 20:39:42 -0700275 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 Schuhe84c3ed2019-12-14 15:29:48 -0800281
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 Schuh507f7582021-07-31 20:39:55 -0700294void 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 Schraderd2a5f5d2023-12-22 11:25:04 -0800300 // Set up Type-Of-Service.
301 //
302 // See comments for the --sctp_tos flag for more information.
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700303 int tos = IPTOS_DSCP(absl::GetFlag(FLAGS_sctp_tos));
Philipp Schraderd2a5f5d2023-12-22 11:25:04 -0800304 PCHECK(setsockopt(fd_, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) == 0);
305 }
306 {
Austin Schuh507f7582021-07-31 20:39:55 -0700307 // Per https://tools.ietf.org/html/rfc6458
308 // Setting this to !0 allows event notifications to be interleaved
Austin Schuha705d782021-07-31 20:40:00 -0700309 // 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 Schuh507f7582021-07-31 20:39:55 -0700317 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 Schuha705d782021-07-31 20:40:00 -0700327 {
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 Snaiderbe263512023-05-18 20:40:23 -0700335 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_EVENTS, (char *)&subscribe,
Austin Schuha705d782021-07-31 20:40:00 -0700336 sizeof(subscribe)) == 0);
337 }
338
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700339#if HAS_SCTP_AUTH
Adam Snaider9bb33442023-06-26 16:31:37 -0700340 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 Snaider96a0f4b2023-05-18 20:41:19 -0700344
Adam Snaider9bb33442023-06-26 16:31:37 -0700345 // 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 Snaider96a0f4b2023-05-18 20:41:19 -0700349
Adam Snaider9bb33442023-06-26 16:31:37 -0700350 // Disallow the null key.
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700351 struct sctp_authkeyid authkeyid;
Adam Snaider9bb33442023-06-26 16:31:37 -0700352 authkeyid.scact_keynumber = 0;
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700353 authkeyid.scact_assoc_id = SCTP_ALL_ASSOC;
Adam Snaider9bb33442023-06-26 16:31:37 -0700354 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY, &authkeyid,
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700355 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 Snaider96a0f4b2023-05-18 20:41:19 -0700363 }
Adam Snaider9bb33442023-06-26 16:31:37 -0700364#endif
Adam Snaider96a0f4b2023-05-18 20:41:19 -0700365
Austin Schuh507f7582021-07-31 20:39:55 -0700366 DoSetMaxSize();
367}
368
369bool 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 Snaider9bb33442023-06-26 16:31:37 -0700374 LOG_IF(FATAL, sctp_authentication_ && current_key_.empty())
375 << "Expected SCTP authentication but no key active";
Austin Schuh507f7582021-07-31 20:39:55 -0700376 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 Kuszmaul784deb72023-02-17 14:42:51 -0800382 memset(&outmsg, 0, sizeof(outmsg));
Austin Schuh507f7582021-07-31 20:39:55 -0700383 if (sockaddr_remote) {
384 outmsg.msg_name = &*sockaddr_remote;
385 outmsg.msg_namelen = sizeof(*sockaddr_remote);
Sarah Newman4aeb2372022-04-06 13:07:11 -0700386 VLOG(2) << "Sending to " << Address(*sockaddr_remote);
Austin Schuh507f7582021-07-31 20:39:55 -0700387 } 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 Schuh581fab92022-02-07 19:50:54 -0800420 if (VLOG_IS_ON(1)) {
421 PLOG(WARNING) << "sendmsg on sctp socket failed";
422 }
Austin Schuh507f7582021-07-31 20:39:55 -0700423 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 Newman4aeb2372022-04-06 13:07:11 -0700429 VLOG(2) << "Sent " << data.size();
Austin Schuh507f7582021-07-31 20:39:55 -0700430 return true;
431}
432
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700433void SctpReadWrite::FreeMessage(aos::unique_c_ptr<Message> &&message) {
434 if (use_pool_) {
435 free_messages_.emplace_back(std::move(message));
436 }
437}
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800438
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700439void 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
448aos::unique_c_ptr<Message> SctpReadWrite::AcquireMessage() {
449 if (!use_pool_) {
James Kuszmaul6e622382023-02-17 14:56:38 -0800450 constexpr size_t kMessageAlign = alignof(Message);
451 const size_t max_message_size =
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700452 ((sizeof(Message) + max_read_size_ + 1 + (kMessageAlign - 1)) /
James Kuszmaul6e622382023-02-17 14:56:38 -0800453 kMessageAlign) *
454 kMessageAlign;
455 aos::unique_c_ptr<Message> result(reinterpret_cast<Message *>(
456 aligned_alloc(kMessageAlign, max_message_size)));
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700457 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.
468aos::unique_c_ptr<Message> SctpReadWrite::ReadMessage() {
469 CHECK(fd_ != -1);
Adam Snaider9bb33442023-06-26 16:31:37 -0700470 LOG_IF(FATAL, sctp_authentication_ && current_key_.empty())
471 << "Expected SCTP authentication but no key active";
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700472
473 while (true) {
474 aos::unique_c_ptr<Message> result = AcquireMessage();
Austin Schuha705d782021-07-31 20:40:00 -0700475
Austin Schuh05c18122021-07-31 20:39:47 -0700476 struct msghdr inmessage;
Austin Schuhc4202572021-03-31 21:06:55 -0700477 memset(&inmessage, 0, sizeof(struct msghdr));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800478
Austin Schuh05c18122021-07-31 20:39:47 -0700479 struct iovec iov;
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700480 iov.iov_len = max_read_size_ + 1;
Austin Schuha705d782021-07-31 20:40:00 -0700481 iov.iov_base = result->mutable_data();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800482
Austin Schuhc4202572021-03-31 21:06:55 -0700483 inmessage.msg_iov = &iov;
484 inmessage.msg_iovlen = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800485
Austin Schuh05c18122021-07-31 20:39:47 -0700486 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
Austin Schuhc4202572021-03-31 21:06:55 -0700487 inmessage.msg_control = incmsg;
488 inmessage.msg_controllen = sizeof(incmsg);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800489
Austin Schuhc4202572021-03-31 21:06:55 -0700490 inmessage.msg_namelen = sizeof(struct sockaddr_storage);
491 inmessage.msg_name = &result->sin;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800492
Austin Schuha705d782021-07-31 20:40:00 -0700493 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 Schuhc4202572021-03-31 21:06:55 -0700498 }
Austin Schuha705d782021-07-31 20:40:00 -0700499 PLOG(FATAL) << "recvmsg on sctp socket " << fd_ << " failed";
Austin Schuhc4202572021-03-31 21:06:55 -0700500 }
501
Austin Schuha705d782021-07-31 20:40:00 -0700502 CHECK(!(inmessage.msg_flags & MSG_CTRUNC))
Austin Schuhc4202572021-03-31 21:06:55 -0700503 << ": Control message truncated.";
504
Austin Schuha705d782021-07-31 20:40:00 -0700505 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 Schuhc4202572021-03-31 21:06:55 -0700511
Austin Schuha705d782021-07-31 20:40:00 -0700512 {
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 Schuh89f23e32023-05-15 17:06:43 -0700532
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 Schuha705d782021-07-31 20:40:00 -0700546 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 Schuh89e1e9c2023-05-15 14:38:44 -0700582 CHECK_LE(partial_message->size + result->size, max_read_size_)
Austin Schuha705d782021-07-31 20:40:00 -0700583 << ": 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 Newman4aeb2372022-04-06 13:07:11 -0700588 VLOG(2) << "Merged fragment of " << result->size << " after "
Austin Schuha705d782021-07-31 20:40:00 -0700589 << 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 Newman4aeb2372022-04-06 13:07:11 -0700616 VLOG(2) << "Starting fragment for: " << result->header.rcvinfo.rcv_sid
Austin Schuha705d782021-07-31 20:40:00 -0700617 << "," << 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 Schuhe84c3ed2019-12-14 15:29:48 -0800622 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800623}
624
Sarah Newman80e955e2022-04-13 11:19:36 -0700625bool 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 Kuszmaul784deb72023-02-17 14:42:51 -0800633 memset(&outmsg, 0, sizeof(outmsg));
Sarah Newman80e955e2022-04-13 11:19:36 -0700634 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 Schuh507f7582021-07-31 20:39:55 -0700668void SctpReadWrite::CloseSocket() {
669 if (fd_ == -1) {
670 return;
671 }
672 LOG(INFO) << "close(" << fd_ << ")";
673 PCHECK(close(fd_) == 0);
674 fd_ = -1;
675}
676
677void SctpReadWrite::DoSetMaxSize() {
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700678 size_t max_size = max_write_size_;
Austin Schuh507f7582021-07-31 20:39:55 -0700679
Austin Schuh61224882021-10-11 18:21:11 -0700680 // This sets the max packet size that we can send.
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700681 CHECK_GE(ReadWMemMax(), max_write_size_)
Austin Schuh507f7582021-07-31 20:39:55 -0700682 << "wmem_max is too low. To increase wmem_max temporarily, do sysctl "
683 "-w net.core.wmem_max="
684 << max_size;
Austin Schuh507f7582021-07-31 20:39:55 -0700685 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_SNDBUF, &max_size, sizeof(max_size)) ==
686 0);
Austin Schuh61224882021-10-11 18:21:11 -0700687
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 Schuh99f7c6a2024-06-25 22:07:44 -0700691 if (absl::GetFlag(FLAGS_rmem) > 0) {
692 size_t rmem = absl::GetFlag(FLAGS_rmem);
Austin Schuh9dd8f592021-12-25 14:32:43 -0800693 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_RCVBUF, &rmem, sizeof(rmem)) == 0);
694 }
Austin Schuh507f7582021-07-31 20:39:55 -0700695}
696
Austin Schuha705d782021-07-31 20:40:00 -0700697bool 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 Snaider9bb33442023-06-26 16:31:37 -0700737void 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 Snaider82d1dc72023-09-26 09:13:55 -0700754 // 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 Snaider9bb33442023-06-26 16:31:37 -0700759
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 Schuhe84c3ed2019-12-14 15:29:48 -0800789void 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 Schuh2fe4b712020-03-15 14:21:45 -0700798size_t ReadRMemMax() {
799 struct stat current_stat;
800 if (stat("/proc/sys/net/core/rmem_max", &current_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
810size_t ReadWMemMax() {
811 struct stat current_stat;
812 if (stat("/proc/sys/net/core/wmem_max", &current_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 Pleinesf63bde82024-01-13 15:59:33 -0800822} // namespace aos::message_bridge