Skip to main content

netstack3_filter/
state.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
5pub mod validation;
6
7use alloc::format;
8use alloc::string::ToString as _;
9use alloc::sync::Arc;
10use alloc::vec::Vec;
11use core::fmt::Debug;
12use core::hash::{Hash, Hasher};
13use core::num::NonZeroU16;
14use core::ops::RangeInclusive;
15
16use derivative::Derivative;
17use net_types::ip::{GenericOverIp, Ip};
18use netstack3_base::{
19    CoreTimerContext, Inspectable, InspectableValue, Inspector as _, MarkDomain, Marks,
20    MatcherBindingsTypes, TimerContext,
21};
22use packet_formats::ip::IpExt;
23
24use crate::actions::MarkAction;
25use crate::conntrack::{self, ConnectionDirection};
26use crate::context::{FilterBindingsTypes, SocketInfo};
27use crate::logic::FilterTimerId;
28use crate::logic::nat::NatConfig;
29use crate::matchers::PacketMatcher;
30use crate::state::validation::ValidRoutines;
31
32/// The action to take on a packet.
33#[derive(Derivative)]
34#[derivative(Clone(bound = "RuleInfo: Clone"), Debug(bound = ""))]
35pub enum Action<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> {
36    /// Accept the packet.
37    ///
38    /// This is a terminal action for the current *installed* routine, i.e. no
39    /// further rules will be evaluated for this packet in the installed routine
40    /// (or any subroutines) in which this rule is installed. Subsequent
41    /// routines installed on the same hook will still be evaluated.
42    Accept,
43    /// Drop the packet.
44    ///
45    /// This is a terminal action for the current hook, i.e. no further rules
46    /// will be evaluated for this packet, even in other routines on the same
47    /// hook.
48    Drop,
49    /// Jump from the current routine to the specified uninstalled routine.
50    Jump(UninstalledRoutine<I, BT, RuleInfo>),
51    /// Stop evaluation of the current routine and return to the calling routine
52    /// (the routine from which the current routine was jumped), continuing
53    /// evaluation at the next rule.
54    ///
55    /// If invoked in an installed routine, equivalent to `Accept`, given
56    /// packets are accepted by default in the absence of any matching rules.
57    Return,
58    /// Redirect the packet to a local socket without changing the packet header
59    /// in any way.
60    ///
61    /// This is a terminal action for the current hook, i.e. no further rules
62    /// will be evaluated for this packet, even in other routines on the same
63    /// hook. However, note that this does not preclude actions on *other* hooks
64    /// from having an effect on this packet; for example, a packet that hits
65    /// TransparentProxy in INGRESS could still be dropped in LOCAL_INGRESS.
66    ///
67    /// This action is only valid in the INGRESS hook. This action is also only
68    /// valid in a rule that ensures the presence of a TCP or UDP header by
69    /// matching on the transport protocol, so that the packet can be properly
70    /// dispatched.
71    ///
72    /// Also note that transparently proxied packets will only be delivered to
73    /// sockets with the transparent socket option enabled.
74    TransparentProxy(TransparentProxy<I>),
75    /// A special case of destination NAT (DNAT) that redirects the packet to
76    /// the local host.
77    ///
78    /// This is a terminal action for all NAT routines on the current hook. The
79    /// packet is redirected by rewriting the destination IP address to one
80    /// owned by the ingress interface (if operating on incoming traffic in
81    /// INGRESS) or the loopback address (if operating on locally-generated
82    /// traffic in LOCAL_EGRESS). If this rule is installed on INGRESS and no IP
83    /// address is assigned to the incoming interface, the packet is dropped.
84    ///
85    /// As with all DNAT actions, this action is only valid in the INGRESS and
86    /// LOCAL_EGRESS hooks. If a destination port is specified, this action is
87    /// only valid in a rule that ensures the presence of a TCP or UDP header by
88    /// matching on the transport protocol, so that the destination port can be
89    /// rewritten.
90    ///
91    /// This is analogous to the `redirect` statement in Netfilter.
92    Redirect {
93        /// The optional range of destination ports used to rewrite the packet.
94        ///
95        /// If specified, the destination port of the packet will be rewritten
96        /// to some randomly chosen port in the range. If absent, the
97        /// destination port of the packet will not be rewritten.
98        dst_port: Option<RangeInclusive<NonZeroU16>>,
99    },
100    /// A special case of source NAT (SNAT) that reassigns the source IP address
101    /// of the packet to an address that is assigned to the outgoing interface.
102    ///
103    /// This is a terminal action for all NAT routines on the current hook. If
104    /// no address is assigned to the outgoing interface, the packet will be
105    /// dropped.
106    ///
107    /// This action is only valid in the EGRESS hook. If a source port range is
108    /// specified, this action is only valid in a rule that ensures the presence
109    /// of a TCP or UDP header by matching on the transport protocol, so that
110    /// the source port can be rewritten.
111    ///
112    /// This is analogous to the `masquerade` statement in Netfilter.
113    Masquerade {
114        /// The optional range of source ports used to rewrite the packet.
115        ///
116        /// The source port will be rewritten if necessary to ensure the
117        /// packet's flow does not conflict with an existing tracked connection.
118        /// Note that the source port may be rewritten whether or not this range
119        /// is specified.
120        ///
121        /// If specified, this overrides the default behavior and restricts the
122        /// range of possible values to which the source port can be rewritten.
123        src_port: Option<RangeInclusive<NonZeroU16>>,
124    },
125    /// Applies the mark action to the given mark domain.
126    ///
127    /// This is a non-terminal action for both routines and hooks. This is also
128    /// only available in [`IpRoutines`] because [`NatRoutines`] only runs on
129    /// the first packet in a connection and it is likely a misconfiguration
130    /// that packets after the first are marked differently or unmarked.
131    ///
132    /// Note: If we find use cases that justify this being in [`NatRoutines`] we
133    /// should relax this limitation and support it.
134    ///
135    /// This is analogous to the `mark` statement in Netfilter.
136    Mark {
137        /// The domain to apply the mark action.
138        domain: MarkDomain,
139        /// The action to apply.
140        action: MarkAction,
141    },
142
143    /// No action.
144    None,
145
146    /// Reject the packet.
147    Reject(RejectType),
148}
149
150/// Transparently intercept the packet and deliver it to a local socket without
151/// changing the packet header.
152///
153/// When a local address is specified, it is the bound address of the local
154/// socket to redirect the packet to. When absent, the destination IP address of
155/// the packet is used for local delivery.
156///
157/// When a local port is specified, it is the bound port of the local socket to
158/// redirect the packet to. When absent, the destination port of the packet is
159/// used for local delivery.
160#[derive(Debug, Clone)]
161#[allow(missing_docs)]
162pub enum TransparentProxy<I: IpExt> {
163    LocalAddr(I::Addr),
164    LocalPort(NonZeroU16),
165    LocalAddrAndPort(I::Addr, NonZeroU16),
166}
167
168impl<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> Inspectable for Action<I, BT, RuleInfo> {
169    fn record<Inspector: netstack3_base::Inspector>(&self, inspector: &mut Inspector) {
170        let value = match self {
171            Self::Accept
172            | Self::Drop
173            | Self::Return
174            | Self::TransparentProxy(_)
175            | Self::Redirect { .. }
176            | Self::Masquerade { .. }
177            | Self::Mark { .. }
178            | Self::None
179            | Self::Reject(_) => {
180                format!("{self:?}")
181            }
182            Self::Jump(UninstalledRoutine { routine: _, id }) => {
183                format!("Jump(UninstalledRoutine({id:?}))")
184            }
185        };
186        inspector.record_string("action", value);
187    }
188}
189
190/// IPv4-specific reject types.
191#[derive(Debug, Clone, Copy, Eq, PartialEq)]
192pub enum RejectType {
193    /// Send a TCP RST packet.
194    TcpReset,
195
196    /// Network unreachable.
197    ///
198    /// IPv4: ICMP Destination network unreachable (type 3, code 0).
199    /// IPv6: ICMPv6 No route to destination (type 1, code 1).
200    NetUnreachable,
201
202    /// Host unreachable.
203    ///
204    /// IPv4: ICMP Host unreachable (type 3, code 1).
205    /// IPv6: ICMPv6 Address unreachable (type 1, code 3).
206    HostUnreachable,
207
208    /// Protocol unreachable.
209    ///
210    /// IPv4: ICMP Protocol unreachable (type 3, code 2).
211    /// IPv6: ICMPv6 Unrecognized Next Header type encountered (type 4, code 1).
212    ProtoUnreachable,
213
214    /// Port unreachable.
215    ///
216    /// IPv4: ICMP Port unreachable (type 3, code 3).
217    /// IPv6: ICMPv6 Port unreachable (type 1, code 4).
218    PortUnreachable,
219
220    /// Route to the destination network is prohibited.
221    ///
222    /// IPv4: ICMP Network administratively prohibited (type 3, code 9).
223    /// IPv6: ICMPv6 Source address failed ingress/egress policy (type 1, code 5).
224    RoutePolicyFail,
225
226    /// Reject route.
227    ///
228    /// IPv4: ICMP Host administratively prohibited (type 3, code 10).
229    /// IPv6: ICMPv6 Reject route to destination (type 1, code 6).
230    RejectRoute,
231
232    /// Communication administratively prohibited.
233    ///
234    /// IPv4: ICMP Communication administratively prohibited (type 3, code 13).
235    /// IPv6: ICMPv6 Communication administratively prohibited (type 1, code 1).
236    AdminProhibited,
237}
238
239/// A handle to a [`Routine`] that is not installed in a particular hook, and
240/// therefore is only run if jumped to from another routine.
241#[derive(Derivative)]
242#[derivative(Clone(bound = ""), Debug(bound = ""))]
243pub struct UninstalledRoutine<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> {
244    pub(crate) routine: Arc<Routine<I, BT, RuleInfo>>,
245    id: usize,
246}
247
248impl<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> UninstalledRoutine<I, BT, RuleInfo> {
249    /// Creates a new uninstalled routine with the provided contents.
250    pub fn new(rules: Vec<Rule<I, BT, RuleInfo>>, id: usize) -> Self {
251        Self { routine: Arc::new(Routine { rules }), id }
252    }
253
254    /// Returns the inner routine.
255    pub fn get(&self) -> &Routine<I, BT, RuleInfo> {
256        &*self.routine
257    }
258}
259
260impl<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> PartialEq
261    for UninstalledRoutine<I, BT, RuleInfo>
262{
263    fn eq(&self, other: &Self) -> bool {
264        let Self { routine: lhs, id: _ } = self;
265        let Self { routine: rhs, id: _ } = other;
266        Arc::ptr_eq(lhs, rhs)
267    }
268}
269
270impl<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> Eq for UninstalledRoutine<I, BT, RuleInfo> {}
271
272impl<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> Hash for UninstalledRoutine<I, BT, RuleInfo> {
273    fn hash<H: Hasher>(&self, state: &mut H) {
274        let Self { routine, id: _ } = self;
275        Arc::as_ptr(routine).hash(state)
276    }
277}
278
279impl<I: IpExt, BT: MatcherBindingsTypes> Inspectable for UninstalledRoutine<I, BT, ()> {
280    fn record<Inspector: netstack3_base::Inspector>(&self, inspector: &mut Inspector) {
281        let Self { routine, id } = self;
282        inspector.record_child(&id.to_string(), |inspector| {
283            inspector.delegate_inspectable(&**routine);
284        });
285    }
286}
287
288/// A set of criteria (matchers) and a resultant action to take if a given
289/// packet matches.
290#[derive(Derivative, GenericOverIp)]
291#[generic_over_ip(I, Ip)]
292#[derivative(Clone(bound = "RuleInfo: Clone"), Debug(bound = ""))]
293pub struct Rule<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> {
294    /// The criteria that a packet must match for the action to be executed.
295    pub matcher: PacketMatcher<I, BT>,
296    /// The action to take on a matching packet.
297    pub action: Action<I, BT, RuleInfo>,
298    /// Opaque information about this rule for use when validating and
299    /// converting state provided by Bindings into Core filtering state. This is
300    /// only used when installing filtering state, and allows Core to report to
301    /// Bindings which rule caused a particular error. It is zero-sized for
302    /// validated state.
303    #[derivative(Debug = "ignore")]
304    pub validation_info: RuleInfo,
305}
306
307impl<I: IpExt, BT: MatcherBindingsTypes> Inspectable for Rule<I, BT, ()> {
308    fn record<Inspector: netstack3_base::Inspector>(&self, inspector: &mut Inspector) {
309        let Self { matcher, action, validation_info: () } = self;
310        inspector.record_child("matchers", |inspector| {
311            let PacketMatcher {
312                in_interface,
313                out_interface,
314                src_address,
315                dst_address,
316                transport_protocol,
317                external_matcher,
318            } = matcher;
319
320            fn record_matcher<Inspector: netstack3_base::Inspector, M: InspectableValue>(
321                inspector: &mut Inspector,
322                name: &str,
323                matcher: &Option<M>,
324            ) {
325                if let Some(matcher) = matcher {
326                    inspector.record_inspectable_value(name, matcher);
327                }
328            }
329
330            record_matcher(inspector, "in_interface", in_interface);
331            record_matcher(inspector, "out_interface", out_interface);
332            record_matcher(inspector, "src_address", src_address);
333            record_matcher(inspector, "dst_address", dst_address);
334            record_matcher(inspector, "transport_protocol", transport_protocol);
335            record_matcher(inspector, "external_matcher", external_matcher);
336        });
337        inspector.delegate_inspectable(action);
338    }
339}
340
341/// A sequence of [`Rule`]s.
342#[derive(Derivative, GenericOverIp)]
343#[generic_over_ip(I, Ip)]
344#[derivative(Clone(bound = "RuleInfo: Clone"), Debug(bound = ""))]
345pub struct Routine<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> {
346    /// The rules to be executed in order.
347    pub rules: Vec<Rule<I, BT, RuleInfo>>,
348}
349
350impl<I: IpExt, BT: MatcherBindingsTypes> Inspectable for Routine<I, BT, ()> {
351    fn record<Inspector: netstack3_base::Inspector>(&self, inspector: &mut Inspector) {
352        let Self { rules } = self;
353        inspector.record_usize("rules", rules.len());
354        for rule in rules {
355            inspector.record_unnamed_child(|inspector| inspector.delegate_inspectable(rule));
356        }
357    }
358}
359
360/// A particular entry point for packet processing in which filtering routines
361/// are installed.
362#[derive(Derivative, GenericOverIp)]
363#[generic_over_ip(I, Ip)]
364#[derivative(Default(bound = ""), Debug(bound = ""))]
365pub struct Hook<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> {
366    /// The routines to be executed in order.
367    pub routines: Vec<Routine<I, BT, RuleInfo>>,
368}
369
370impl<I: IpExt, BT: MatcherBindingsTypes> Inspectable for Hook<I, BT, ()> {
371    fn record<Inspector: netstack3_base::Inspector>(&self, inspector: &mut Inspector) {
372        let Self { routines } = self;
373        inspector.record_usize("routines", routines.len());
374        for routine in routines {
375            inspector.record_unnamed_child(|inspector| {
376                inspector.delegate_inspectable(routine);
377            });
378        }
379    }
380}
381
382/// Routines that perform ordinary IP filtering.
383#[derive(Derivative)]
384#[derivative(Default(bound = ""), Debug(bound = ""))]
385pub struct IpRoutines<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> {
386    /// Occurs for incoming traffic before a routing decision has been made.
387    pub ingress: Hook<I, BT, RuleInfo>,
388    /// Occurs for incoming traffic that is destined for the local host.
389    pub local_ingress: Hook<I, BT, RuleInfo>,
390    /// Occurs for incoming traffic that is destined for another node.
391    pub forwarding: Hook<I, BT, RuleInfo>,
392    /// Occurs for locally-generated traffic before a final routing decision has
393    /// been made.
394    pub local_egress: Hook<I, BT, RuleInfo>,
395    /// Occurs for all outgoing traffic after a routing decision has been made.
396    pub egress: Hook<I, BT, RuleInfo>,
397}
398
399impl<I: IpExt, BT: MatcherBindingsTypes> Inspectable for IpRoutines<I, BT, ()> {
400    fn record<Inspector: netstack3_base::Inspector>(&self, inspector: &mut Inspector) {
401        let Self { ingress, local_ingress, forwarding, local_egress, egress } = self;
402
403        inspector.record_child("ingress", |inspector| inspector.delegate_inspectable(ingress));
404        inspector.record_child("local_ingress", |inspector| {
405            inspector.delegate_inspectable(local_ingress)
406        });
407        inspector
408            .record_child("forwarding", |inspector| inspector.delegate_inspectable(forwarding));
409        inspector
410            .record_child("local_egress", |inspector| inspector.delegate_inspectable(local_egress));
411        inspector.record_child("egress", |inspector| inspector.delegate_inspectable(egress));
412    }
413}
414
415/// Routines that can perform NAT.
416///
417/// Note that NAT routines are only executed *once* for a given connection, for
418/// the first packet in the flow.
419#[derive(Derivative)]
420#[derivative(Default(bound = ""), Debug(bound = ""))]
421pub struct NatRoutines<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> {
422    /// Occurs for incoming traffic before a routing decision has been made.
423    pub ingress: Hook<I, BT, RuleInfo>,
424    /// Occurs for incoming traffic that is destined for the local host.
425    pub local_ingress: Hook<I, BT, RuleInfo>,
426    /// Occurs for locally-generated traffic before a final routing decision has
427    /// been made.
428    pub local_egress: Hook<I, BT, RuleInfo>,
429    /// Occurs for all outgoing traffic after a routing decision has been made.
430    pub egress: Hook<I, BT, RuleInfo>,
431}
432
433impl<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> NatRoutines<I, BT, RuleInfo> {
434    pub(crate) fn contains_rules(&self) -> bool {
435        let Self { ingress, local_ingress, local_egress, egress } = self;
436
437        let hook_contains_rules =
438            |hook: &Hook<_, _, _>| hook.routines.iter().any(|routine| !routine.rules.is_empty());
439        hook_contains_rules(&ingress)
440            || hook_contains_rules(&local_ingress)
441            || hook_contains_rules(&local_egress)
442            || hook_contains_rules(&egress)
443    }
444}
445
446impl<I: IpExt, BT: MatcherBindingsTypes> Inspectable for NatRoutines<I, BT, ()> {
447    fn record<Inspector: netstack3_base::Inspector>(&self, inspector: &mut Inspector) {
448        let Self { ingress, local_ingress, local_egress, egress } = self;
449
450        inspector.record_child("ingress", |inspector| inspector.delegate_inspectable(ingress));
451        inspector.record_child("local_ingress", |inspector| {
452            inspector.delegate_inspectable(local_ingress)
453        });
454        inspector
455            .record_child("local_egress", |inspector| inspector.delegate_inspectable(local_egress));
456        inspector.record_child("egress", |inspector| inspector.delegate_inspectable(egress));
457    }
458}
459
460/// IP version-specific filtering routine state.
461#[derive(Derivative, GenericOverIp)]
462#[generic_over_ip(I, Ip)]
463#[derivative(Default(bound = ""), Debug(bound = ""))]
464pub struct Routines<I: IpExt, BT: MatcherBindingsTypes, RuleInfo> {
465    /// Routines that perform IP filtering.
466    pub ip: IpRoutines<I, BT, RuleInfo>,
467    /// Routines that perform IP filtering and NAT.
468    pub nat: NatRoutines<I, BT, RuleInfo>,
469}
470
471/// A one-way boolean toggle that can only go from `false` to `true`.
472///
473/// Once it has been flipped to `true`, it will remain in that state forever.
474#[derive(Default)]
475pub struct OneWayBoolean(bool);
476
477impl OneWayBoolean {
478    /// A [`OneWayBoolean`] that is enabled on creation.
479    pub const TRUE: Self = Self(true);
480
481    /// Get the value of the boolean.
482    pub fn get(&self) -> bool {
483        let Self(inner) = self;
484        *inner
485    }
486
487    /// Toggle the boolean to `true`.
488    ///
489    /// This operation is idempotent: even though the [`OneWayBoolean`]'s value will
490    /// only ever change from `false` to `true` once, this method can be called any
491    /// number of times safely and the value will remain `true`.
492    pub fn set(&mut self) {
493        let Self(inner) = self;
494        *inner = true;
495    }
496}
497
498/// IP version-specific filtering state.
499pub struct State<I: IpExt, A, BT: FilterBindingsTypes> {
500    /// Routines used for filtering packets that are installed on hooks.
501    pub installed_routines: ValidRoutines<I, BT>,
502    /// Routines that are only executed if jumped to from other routines.
503    ///
504    /// Jump rules refer to their targets by holding a reference counted pointer
505    /// to the inner routine; we hold this index of all uninstalled routines
506    /// that have any references in order to report them in inspect data.
507    pub(crate) uninstalled_routines: Vec<UninstalledRoutine<I, BT, ()>>,
508    /// Connection tracking state.
509    pub conntrack: conntrack::Table<I, NatConfig<I, A>, BT>,
510    /// One-way boolean toggle indicating whether any rules have ever been added to
511    /// an installed NAT routine. If not, performing NAT can safely be skipped.
512    ///
513    /// This is useful because if any NAT is being performed, we have to check
514    /// whether it's necessary to perform implicit NAT for *all* traffic -- even if
515    /// it doesn't match any NAT rules -- to avoid conflicting tracked connections.
516    /// If we know that no NAT is being performed at all, this extra work can be
517    /// avoided.
518    ///
519    /// Note that this value will only ever go from false to true; it does not
520    /// indicate whether any NAT rules are *currently* installed. This avoids a race
521    /// condition where NAT rules are removed but connections are still being NATed
522    /// based on those rules, and therefore must be considered when creating new
523    /// connection tracking entries.
524    pub nat_installed: OneWayBoolean,
525}
526
527impl<I, A, BC> State<I, A, BC>
528where
529    I: IpExt,
530    BC: FilterBindingsTypes + TimerContext,
531{
532    /// Create a new State.
533    pub fn new<CC: CoreTimerContext<FilterTimerId<I>, BC>>(bindings_ctx: &mut BC) -> Self {
534        Self {
535            installed_routines: Default::default(),
536            uninstalled_routines: Default::default(),
537            conntrack: conntrack::Table::new::<CC>(bindings_ctx),
538            nat_installed: OneWayBoolean::default(),
539        }
540    }
541}
542
543impl<I: IpExt, A: InspectableValue, BT: FilterBindingsTypes> Inspectable for State<I, A, BT> {
544    fn record<Inspector: netstack3_base::Inspector>(&self, inspector: &mut Inspector) {
545        let Self { installed_routines, uninstalled_routines, conntrack, nat_installed: _ } = self;
546        let Routines { ip, nat } = installed_routines.get();
547
548        inspector.record_child("IP", |inspector| inspector.delegate_inspectable(ip));
549        inspector.record_child("NAT", |inspector| inspector.delegate_inspectable(nat));
550        inspector.record_child("uninstalled", |inspector| {
551            inspector.record_usize("routines", uninstalled_routines.len());
552            for routine in uninstalled_routines {
553                inspector.delegate_inspectable(routine);
554            }
555        });
556
557        inspector.record_child("conntrack", |inspector| {
558            inspector.delegate_inspectable(conntrack);
559        });
560    }
561}
562
563/// A trait for interacting with the pieces of packet metadata that are
564/// important for filtering.
565pub trait FilterIpMetadata<I: IpExt, A, BT: FilterBindingsTypes>: FilterPacketMetadata {
566    /// Removes the conntrack connection and packet direction, if they exist.
567    fn take_connection_and_direction(
568        &mut self,
569    ) -> Option<(conntrack::Connection<I, NatConfig<I, A>, BT>, ConnectionDirection)>;
570
571    /// Puts a new conntrack connection and packet direction into the metadata
572    /// struct, returning the previous connection value, if one existed.
573    fn replace_connection_and_direction(
574        &mut self,
575        conn: conntrack::Connection<I, NatConfig<I, A>, BT>,
576        direction: ConnectionDirection,
577    ) -> Option<conntrack::Connection<I, NatConfig<I, A>, BT>>;
578}
579
580/// A trait for interacting with packet metadata.
581//
582// The reason why we split this trait from the `FilterIpMetadata` is to avoid
583// introducing trait bounds and type parameters into functions that don't
584// depend on conntrack, for example, all the `check_routine*` methods. This
585// becomes a meaningful simplification for those cases.
586pub trait FilterPacketMetadata {
587    /// Applies the mark action to the metadata.
588    fn apply_mark_action(&mut self, domain: MarkDomain, action: MarkAction);
589    /// Socket info.
590    fn socket_info(&self) -> Option<SocketInfo>;
591    /// Socket marks.
592    fn marks(&self) -> &Marks;
593}
594
595/// No-op implementation of `FilterIpMetadata` and `FilterPacketMetadata`
596/// traits.
597#[derive(Default)]
598pub struct FakePacketMetadata {
599    marks: Marks,
600}
601
602impl<I: IpExt, A, BT: FilterBindingsTypes> FilterIpMetadata<I, A, BT> for FakePacketMetadata {
603    fn take_connection_and_direction(
604        &mut self,
605    ) -> Option<(conntrack::Connection<I, NatConfig<I, A>, BT>, ConnectionDirection)> {
606        None
607    }
608
609    fn replace_connection_and_direction(
610        &mut self,
611        _conn: conntrack::Connection<I, NatConfig<I, A>, BT>,
612        _direction: ConnectionDirection,
613    ) -> Option<conntrack::Connection<I, NatConfig<I, A>, BT>> {
614        None
615    }
616}
617
618impl FilterPacketMetadata for FakePacketMetadata {
619    fn apply_mark_action(&mut self, _domain: MarkDomain, _action: MarkAction) {}
620
621    fn socket_info(&self) -> Option<SocketInfo> {
622        None
623    }
624
625    fn marks(&self) -> &Marks {
626        &self.marks
627    }
628}