Skip to main content

netstack3_ip/
local_delivery.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Types and helpers used in local-delivery of packets.
6
7use core::num::NonZeroU16;
8
9use net_types::SpecifiedAddr;
10use net_types::ip::{GenericOverIp, Ip, Ipv4, Ipv6};
11use netstack3_base::{IpExt, Marks, NetworkParsingContext};
12use packet_formats::ip::DscpAndEcn;
13use packet_formats::ipv4::Ipv4Header as _;
14use packet_formats::ipv4::options::Ipv4Option;
15use packet_formats::ipv6::Ipv6Header as _;
16use packet_formats::ipv6::ext_hdrs::{HopByHopOptionData, Ipv6ExtensionHeaderData};
17
18/// Informs the transport layer of parameters for transparent local delivery.
19#[derive(Debug, GenericOverIp, Clone)]
20#[generic_over_ip(I, Ip)]
21pub struct TransparentLocalDelivery<I: IpExt> {
22    /// The local delivery address.
23    pub addr: SpecifiedAddr<I::Addr>,
24    /// The local delivery port.
25    pub port: NonZeroU16,
26}
27
28/// Meta information for an incoming packet.
29#[derive(Debug, GenericOverIp, Clone)]
30#[generic_over_ip(I, Ip)]
31pub struct ReceiveIpPacketMeta<I: IpExt> {
32    /// Indicates that the packet was sent to a broadcast address.
33    pub broadcast: Option<I::BroadcastMarker>,
34
35    /// Destination overrides for the transparent proxy.
36    pub transparent_override: Option<TransparentLocalDelivery<I>>,
37
38    /// The parsing context for the received packet.
39    pub parsing_context: NetworkParsingContext,
40}
41
42/// Information for an incoming packet.
43///
44/// This is given to upper layers to provide extra information on locally
45/// delivered IP packets.
46#[derive(Debug, Clone)]
47pub struct LocalDeliveryPacketInfo<I: IpExt, H: IpHeaderInfo<I>> {
48    /// Packet metadata calculated by the stack.
49    pub meta: ReceiveIpPacketMeta<I>,
50    /// Accessor for extra information in IP header.
51    pub header_info: H,
52    /// The marks carried by the incoming packet.
53    pub marks: Marks,
54}
55
56/// Abstracts extracting information from IP headers for upper layers.
57///
58/// Implemented for the combination of fixed header and options of IPv4 and IPv6
59/// packets, so we can ensure this information is always extracted from packets,
60/// including when they're rewritten by filters.
61///
62/// This is a trait so:
63/// - We're gating here and acknowledging all the information necessary by upper
64///   layers.
65/// - A [fake implementation] can be provided without constructing a full
66///   IPv4/IPv6 packet.
67///
68/// [fake implementation]: testutil::FakeIpHeaderInfo
69pub trait IpHeaderInfo<I> {
70    /// DSCP and ECN values received in Traffic Class or TOS field.
71    fn dscp_and_ecn(&self) -> DscpAndEcn;
72
73    /// The TTL (IPv4) or Hop Limit (IPv6) of the received packet.
74    fn hop_limit(&self) -> u8;
75
76    /// Returns true if the router alert option (IPv4) or extension header
77    /// (IPv6) is present on the packet.
78    fn router_alert(&self) -> bool;
79
80    /// Returns the IP header as bytes.
81    fn as_bytes(&self) -> [&[u8]; 2];
82}
83
84pub(crate) struct Ipv4HeaderInfo<'a> {
85    pub(crate) prefix: &'a packet_formats::ipv4::HeaderPrefix,
86    pub(crate) options: packet_formats::ipv4::Options<&'a [u8]>,
87}
88
89impl IpHeaderInfo<Ipv4> for Ipv4HeaderInfo<'_> {
90    fn dscp_and_ecn(&self) -> DscpAndEcn {
91        self.prefix.dscp_and_ecn()
92    }
93
94    fn hop_limit(&self) -> u8 {
95        self.prefix.ttl()
96    }
97
98    fn router_alert(&self) -> bool {
99        self.options.iter().any(|opt| matches!(opt, Ipv4Option::RouterAlert { .. }))
100    }
101
102    fn as_bytes(&self) -> [&[u8]; 2] {
103        use zerocopy::IntoBytes as _;
104        let Self { prefix, options } = self;
105        [prefix.as_bytes(), options.bytes()]
106    }
107}
108
109pub(crate) struct Ipv6HeaderInfo<'a> {
110    pub(crate) fixed: &'a packet_formats::ipv6::FixedHeader,
111    pub(crate) extension: packet_formats::ipv6::ExtensionHeaders<'a>,
112}
113
114impl IpHeaderInfo<Ipv6> for Ipv6HeaderInfo<'_> {
115    fn dscp_and_ecn(&self) -> DscpAndEcn {
116        self.fixed.dscp_and_ecn()
117    }
118
119    fn hop_limit(&self) -> u8 {
120        self.fixed.hop_limit()
121    }
122
123    fn router_alert(&self) -> bool {
124        self.extension.iter().any(|h| match h.data() {
125            Ipv6ExtensionHeaderData::HopByHopOptions { options } => {
126                options.iter().any(|h| matches!(h.data, HopByHopOptionData::RouterAlert { .. }))
127            }
128            _ => false,
129        })
130    }
131
132    fn as_bytes(&self) -> [&[u8]; 2] {
133        use zerocopy::IntoBytes as _;
134        let Self { fixed, extension } = self;
135        [fixed.as_bytes(), extension.bytes()]
136    }
137}
138
139#[cfg(any(test, feature = "testutils"))]
140pub(crate) mod testutil {
141    use super::*;
142    use alloc::vec::Vec;
143
144    /// Handroll a default impl for `ReceiveIpPacketMeta` only for tests to
145    /// prevent accidental usage.
146    impl<I: IpExt> Default for ReceiveIpPacketMeta<I> {
147        fn default() -> Self {
148            Self {
149                broadcast: None,
150                transparent_override: None,
151                parsing_context: NetworkParsingContext::default(),
152            }
153        }
154    }
155
156    impl<I: IpExt> Default for LocalDeliveryPacketInfo<I, FakeIpHeaderInfo> {
157        fn default() -> Self {
158            Self {
159                meta: Default::default(),
160                header_info: Default::default(),
161                marks: Default::default(),
162            }
163        }
164    }
165
166    /// A fake implementation of [`IpHeaderInfo`].
167    #[derive(Debug, Default, Clone)]
168    pub struct FakeIpHeaderInfo {
169        /// The value returned by [`IpHeaderInfo::dscp_and_ecn`].
170        pub dscp_and_ecn: DscpAndEcn,
171        /// The value returned by [`IpHeaderInfo::hop_limit`].
172        pub hop_limit: u8,
173        /// The value returned by [`IpHeaderInfo::router_alert`].
174        pub router_alert: bool,
175        /// The value returned by [`IpHeaderInfo::as_bytes`].
176        pub as_bytes: [Vec<u8>; 2],
177    }
178
179    impl<I: IpExt> IpHeaderInfo<I> for FakeIpHeaderInfo {
180        fn dscp_and_ecn(&self) -> DscpAndEcn {
181            self.dscp_and_ecn
182        }
183
184        fn hop_limit(&self) -> u8 {
185            self.hop_limit
186        }
187
188        fn router_alert(&self) -> bool {
189            self.router_alert
190        }
191
192        fn as_bytes(&self) -> [&[u8]; 2] {
193            [&self.as_bytes[0], &self.as_bytes[1]]
194        }
195    }
196}