From 70fca4ccbe11572ad053c64d2ea9deaef8f6f17e Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Thu, 29 Jun 2023 14:35:36 +0200 Subject: [PATCH 1/8] Parse SOL_TLS control message, closes #2064 --- src/sys/socket/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index b34c4c3fdb..622b2dd1ed 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -868,6 +868,11 @@ pub enum ControlMessageOwned { #[cfg_attr(docsrs, doc(cfg(feature = "net")))] Ipv6RecvErr(libc::sock_extended_err, Option), + /// `SOL_TLS` messages of type `TLS_GET_RECORD_TYPE`, containing the TLS message content type, + /// normally one of change_cipher_spec(20), alert(21), handshake(22) (for TLS 1.3 + /// resumption tickets), application_data(23) + TlsGetRecordType(u8), + /// Catch-all variant for unimplemented cmsg types. #[doc(hidden)] Unknown(UnknownCmsg), @@ -885,6 +890,12 @@ pub struct Timestamps { pub hw_raw: TimeSpec, } +// Defined in `linux/tls.h` +#[cfg(all(target_os = "linux"))] +const TLS_GET_RECORD_TYPE: c_int = 2; + +const SOL_TLS: c_int = 282; + impl ControlMessageOwned { /// Decodes a `ControlMessageOwned` from raw bytes. /// @@ -1027,6 +1038,11 @@ impl ControlMessageOwned { let dl = ptr::read_unaligned(p as *const libc::sockaddr_in6); ControlMessageOwned::Ipv6OrigDstAddr(dl) }, + #[cfg(all(target_os = "linux"))] + (SOL_TLS, TLS_GET_RECORD_TYPE) => { + let content_type = ptr::read_unaligned(p as *const u8); + ControlMessageOwned::TlsGetRecordType(content_type) + }, (_, _) => { let sl = std::slice::from_raw_parts(p, len); let ucmsg = UnknownCmsg(*header, Vec::::from(sl)); From ac7b1d50d019b5da964efcc4d6d774d2e938e697 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Thu, 29 Jun 2023 14:41:22 +0200 Subject: [PATCH 2/8] Only Linux gets SOL_TLS I guess (this is wrong, BSDs have it too) --- src/sys/socket/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 622b2dd1ed..9e8847f216 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -894,6 +894,7 @@ pub struct Timestamps { #[cfg(all(target_os = "linux"))] const TLS_GET_RECORD_TYPE: c_int = 2; +#[cfg(all(target_os = "linux"))] const SOL_TLS: c_int = 282; impl ControlMessageOwned { From 981b07ef3d478ed599e23de5c908fb6fd371b7b3 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Wed, 13 Sep 2023 12:30:15 +0200 Subject: [PATCH 3/8] Use libc constants --- Cargo.toml | 2 +- src/sys/socket/mod.rs | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5a7806075a..9d34df18e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ targets = [ ] [dependencies] -libc = { version = "0.2.147", features = ["extra_traits"] } +libc = { version = "0.2.148", features = ["extra_traits"] } bitflags = "2.3.1" cfg-if = "1.0" pin-utils = { version = "0.1.0", optional = true } diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 9e8847f216..a1087458a8 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -890,13 +890,6 @@ pub struct Timestamps { pub hw_raw: TimeSpec, } -// Defined in `linux/tls.h` -#[cfg(all(target_os = "linux"))] -const TLS_GET_RECORD_TYPE: c_int = 2; - -#[cfg(all(target_os = "linux"))] -const SOL_TLS: c_int = 282; - impl ControlMessageOwned { /// Decodes a `ControlMessageOwned` from raw bytes. /// @@ -1040,7 +1033,7 @@ impl ControlMessageOwned { ControlMessageOwned::Ipv6OrigDstAddr(dl) }, #[cfg(all(target_os = "linux"))] - (SOL_TLS, TLS_GET_RECORD_TYPE) => { + (libc::SOL_TLS, libc::TLS_GET_RECORD_TYPE) => { let content_type = ptr::read_unaligned(p as *const u8); ControlMessageOwned::TlsGetRecordType(content_type) }, From a38be50d7a3284a2bf0e075f4eb9fcad2e7f8e9a Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Sun, 5 Nov 2023 12:28:20 +0000 Subject: [PATCH 4/8] Also parse SOL_TLS on Android --- src/sys/socket/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index a1087458a8..d8f7f0d272 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -1032,7 +1032,7 @@ impl ControlMessageOwned { let dl = ptr::read_unaligned(p as *const libc::sockaddr_in6); ControlMessageOwned::Ipv6OrigDstAddr(dl) }, - #[cfg(all(target_os = "linux"))] + #[cfg(any(target_os = "android", target_os = "linux"))] (libc::SOL_TLS, libc::TLS_GET_RECORD_TYPE) => { let content_type = ptr::read_unaligned(p as *const u8); ControlMessageOwned::TlsGetRecordType(content_type) From 88e2e575bad0f6c1efd7f1fb03a90ce4ce223f59 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Sun, 5 Nov 2023 12:39:57 +0000 Subject: [PATCH 5/8] Decode TLS record types --- src/sys/socket/mod.rs | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index d8f7f0d272..be593dfbb6 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -868,10 +868,8 @@ pub enum ControlMessageOwned { #[cfg_attr(docsrs, doc(cfg(feature = "net")))] Ipv6RecvErr(libc::sock_extended_err, Option), - /// `SOL_TLS` messages of type `TLS_GET_RECORD_TYPE`, containing the TLS message content type, - /// normally one of change_cipher_spec(20), alert(21), handshake(22) (for TLS 1.3 - /// resumption tickets), application_data(23) - TlsGetRecordType(u8), + /// `SOL_TLS` messages of type `TLS_GET_RECORD_TYPE` + TlsGetRecordType(TlsGetRecordType), /// Catch-all variant for unimplemented cmsg types. #[doc(hidden)] @@ -890,6 +888,33 @@ pub struct Timestamps { pub hw_raw: TimeSpec, } +/// These constants correspond to TLS 1.2 message types, as defined in +/// RFC 5246, Appendix A.1 +#[cfg(any(target_os = "android", target_os = "linux"))] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[repr(u8)] +#[non_exhaustive] +pub enum TlsGetRecordType { + ChangeCipherSpec , + Alert, + Handshake, + ApplicationData, + Unknown(u8), +} + +#[cfg(any(target_os = "android", target_os = "linux"))] +impl From for TlsGetRecordType { + fn from(x: u8) -> Self { + match x { + 20 => TlsGetRecordType::ChangeCipherSpec, + 21 => TlsGetRecordType::Alert, + 22 => TlsGetRecordType::Handshake, + 23 => TlsGetRecordType::ApplicationData, + _ => TlsGetRecordType::Unknown(x), + } + } +} + impl ControlMessageOwned { /// Decodes a `ControlMessageOwned` from raw bytes. /// @@ -1035,7 +1060,7 @@ impl ControlMessageOwned { #[cfg(any(target_os = "android", target_os = "linux"))] (libc::SOL_TLS, libc::TLS_GET_RECORD_TYPE) => { let content_type = ptr::read_unaligned(p as *const u8); - ControlMessageOwned::TlsGetRecordType(content_type) + ControlMessageOwned::TlsGetRecordType(content_type.into()) }, (_, _) => { let sl = std::slice::from_raw_parts(p, len); From 35141d4d0f6d5eaa711e09cac94395e9f3914a43 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Mon, 6 Nov 2023 15:52:56 +0000 Subject: [PATCH 6/8] Only have TlsGetRecordType enum variant on supported platforms --- src/sys/socket/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index be593dfbb6..6bad41a58a 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -869,6 +869,7 @@ pub enum ControlMessageOwned { Ipv6RecvErr(libc::sock_extended_err, Option), /// `SOL_TLS` messages of type `TLS_GET_RECORD_TYPE` + #[cfg(any(target_os = "android", target_os = "linux"))] TlsGetRecordType(TlsGetRecordType), /// Catch-all variant for unimplemented cmsg types. From 43162b49cb944fdf35ceef85d22c5bd99ddcfaa3 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Mon, 6 Nov 2023 16:23:10 +0000 Subject: [PATCH 7/8] Remove android from target platforms ...since the corresponding libc constant isn't gated for Android. --- src/sys/socket/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 6bad41a58a..9b1b6c8a41 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -869,7 +869,7 @@ pub enum ControlMessageOwned { Ipv6RecvErr(libc::sock_extended_err, Option), /// `SOL_TLS` messages of type `TLS_GET_RECORD_TYPE` - #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg(any(target_os = "linux"))] TlsGetRecordType(TlsGetRecordType), /// Catch-all variant for unimplemented cmsg types. @@ -891,7 +891,7 @@ pub struct Timestamps { /// These constants correspond to TLS 1.2 message types, as defined in /// RFC 5246, Appendix A.1 -#[cfg(any(target_os = "android", target_os = "linux"))] +#[cfg(any(target_os = "linux"))] #[derive(Clone, Copy, PartialEq, Eq, Debug)] #[repr(u8)] #[non_exhaustive] @@ -903,7 +903,7 @@ pub enum TlsGetRecordType { Unknown(u8), } -#[cfg(any(target_os = "android", target_os = "linux"))] +#[cfg(any(target_os = "linux"))] impl From for TlsGetRecordType { fn from(x: u8) -> Self { match x { @@ -1058,7 +1058,7 @@ impl ControlMessageOwned { let dl = ptr::read_unaligned(p as *const libc::sockaddr_in6); ControlMessageOwned::Ipv6OrigDstAddr(dl) }, - #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg(any(target_os = "linux"))] (libc::SOL_TLS, libc::TLS_GET_RECORD_TYPE) => { let content_type = ptr::read_unaligned(p as *const u8); ControlMessageOwned::TlsGetRecordType(content_type.into()) From 6ad9b161dfc7b7fcba04eee75f08dbff8842d2ab Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Wed, 8 Nov 2023 15:48:27 +0000 Subject: [PATCH 8/8] Add changelog entry --- changelog/2065.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2065.added.md diff --git a/changelog/2065.added.md b/changelog/2065.added.md new file mode 100644 index 0000000000..a3dcd5a833 --- /dev/null +++ b/changelog/2065.added.md @@ -0,0 +1 @@ +Added `TlsGetRecordType` control message type and corresponding enum for linux \ No newline at end of file