netstack3_core/
lib.rs

1// Copyright 2018 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//! A networking stack.
6
7#![no_std]
8// TODO(https://fxbug.dev/339502691): Return to the default limit once lock
9// ordering no longer causes overflows.
10#![recursion_limit = "256"]
11// In case we roll the toolchain and something we're using as a feature has been
12// stabilized.
13#![allow(stable_features)]
14#![warn(
15    missing_docs,
16    unreachable_patterns,
17    clippy::useless_conversion,
18    clippy::redundant_clone,
19    clippy::precedence
20)]
21extern crate alloc;
22
23mod api;
24mod context;
25mod counters;
26mod lock_ordering;
27mod marker;
28mod state;
29mod time;
30mod transport;
31
32#[cfg(any(test, feature = "testutils"))]
33pub mod testutil;
34
35/// Data structures.
36pub mod data_structures {
37    /// Read-copy-update data structures.
38    pub mod rcu {
39        pub use netstack3_base::rcu::{ReadGuard, SynchronizedWriterRcu, WriteGuard};
40    }
41}
42/// The device layer.
43pub mod device {
44    #[path = "."]
45    pub(crate) mod integration {
46        mod base;
47        mod blackhole;
48        mod ethernet;
49        mod loopback;
50        mod pure_ip;
51        mod socket;
52
53        pub(crate) use base::{
54            device_state, device_state_and_core_ctx, get_mtu, ip_device_state,
55            ip_device_state_and_core_ctx,
56        };
57    }
58
59    // Re-exported types.
60    pub use netstack3_base::{
61        BoundInterfaceMatcher, InterfaceMatcher, InterfaceProperties, StrongDeviceIdentifier,
62    };
63    pub use netstack3_device::blackhole::{BlackholeDevice, BlackholeDeviceId};
64    pub use netstack3_device::ethernet::{
65        EthernetCreationProperties, EthernetDeviceEvent, EthernetDeviceId, EthernetLinkDevice,
66        EthernetWeakDeviceId, MaxEthernetFrameSize, RecvEthernetFrameMeta,
67    };
68    pub use netstack3_device::loopback::{
69        LoopbackCreationProperties, LoopbackDevice, LoopbackDeviceId, LoopbackWeakDeviceId,
70    };
71    pub use netstack3_device::pure_ip::{
72        PureIpDevice, PureIpDeviceCreationProperties, PureIpDeviceId,
73        PureIpDeviceReceiveFrameMetadata, PureIpHeaderParams, PureIpWeakDeviceId,
74    };
75    pub use netstack3_device::queue::{
76        BatchSize, ReceiveQueueBindingsContext, TransmitQueueBindingsContext,
77        TransmitQueueConfiguration,
78    };
79    pub use netstack3_device::{
80        ArpConfiguration, ArpConfigurationUpdate, DeviceClassMatcher, DeviceConfiguration,
81        DeviceConfigurationUpdate, DeviceConfigurationUpdateError, DeviceCounters, DeviceId,
82        DeviceIdAndNameMatcher, DeviceLayerEventDispatcher, DeviceLayerStateTypes, DeviceProvider,
83        DeviceSendFrameError, NdpConfiguration, NdpConfigurationUpdate, WeakDeviceId,
84    };
85}
86
87/// Device socket API.
88pub mod device_socket {
89    pub use netstack3_base::{FrameDestination, SendFrameErrorReason};
90    pub use netstack3_device::socket::{
91        DeviceSocketBindingsContext, DeviceSocketMetadata, DeviceSocketTypes, EthernetFrame,
92        EthernetHeaderParams, Frame, IpFrame, Protocol, ReceiveFrameError, ReceivedFrame,
93        SentFrame, SocketId, SocketInfo, TargetDevice, WeakDeviceSocketId,
94    };
95}
96
97/// Generic netstack errors.
98pub mod error {
99    pub use netstack3_base::{
100        AddressResolutionFailed, ExistsError, LocalAddressError, NotFoundError, NotSupportedError,
101        RemoteAddressError, SocketError, ZonedAddressError,
102    };
103}
104
105/// Framework for packet filtering.
106pub mod filter {
107    mod integration;
108
109    pub use netstack3_filter::{
110        Action, BindingsPacketMatcher, FilterApi, FilterBindingsContext, FilterBindingsTypes,
111        FilterIpExt, Hook, IpPacket, IpRoutines, MarkAction, NatRoutines, PacketMatcher,
112        ProofOfEgressCheck, Routine, Routines, Rule, SocketEgressFilterResult,
113        SocketIngressFilterResult, SocketOpsFilter, SocketOpsFilterBindingContext,
114        TransparentProxy, TransportProtocolMatcher, Tuple, UninstalledRoutine, ValidationError,
115    };
116}
117
118/// Facilities for inspecting stack state for debugging.
119pub mod inspect {
120    pub use netstack3_base::{
121        Inspectable, InspectableValue, Inspector, InspectorDeviceExt, InspectorExt,
122    };
123}
124
125/// Methods for dealing with ICMP sockets.
126pub mod icmp {
127    pub use netstack3_icmp_echo::{
128        IcmpEchoBindingsContext, IcmpEchoBindingsTypes, IcmpEchoSettings, IcmpSocketId,
129        ReceiveIcmpEchoError,
130    };
131}
132
133/// The Internet Protocol, versions 4 and 6.
134pub mod ip {
135    #[path = "."]
136    pub(crate) mod integration {
137        mod base;
138        mod device;
139        mod multicast_forwarding;
140        mod raw;
141
142        pub(crate) use device::CoreCtxWithIpDeviceConfiguration;
143    }
144
145    // Re-exported types.
146    pub use netstack3_base::{
147        AddressMatcher, AddressMatcherEither, AddressMatcherType, Mark, MarkDomain,
148        MarkInDomainMatcher, MarkMatcher, MarkMatchers, Marks, PortMatcher, SubnetMatcher,
149        WrapBroadcastMarker,
150    };
151    pub use netstack3_ip::device::{
152        AddIpAddrSubnetError, AddrSubnetAndManualConfigEither, AddressRemovedReason,
153        CommonAddressConfig, CommonAddressProperties, IidGenerationConfiguration, IidSecret,
154        IpAddressState, IpDeviceConfiguration, IpDeviceConfigurationAndFlags,
155        IpDeviceConfigurationUpdate, IpDeviceEvent, IpDeviceIpExt, Ipv4AddrConfig,
156        Ipv4DeviceConfiguration, Ipv4DeviceConfigurationUpdate, Ipv6AddrManualConfig,
157        Ipv6DeviceConfiguration, Ipv6DeviceConfigurationUpdate, Lifetime,
158        PendingIpDeviceConfigurationUpdate, PreferredLifetime, SetIpAddressPropertiesError,
159        SlaacConfiguration, SlaacConfigurationUpdate, StableSlaacAddressConfiguration,
160        TemporarySlaacAddressConfiguration, UpdateIpConfigurationError,
161    };
162    pub use netstack3_ip::gmp::{IgmpConfigMode, MldConfigMode};
163    pub use netstack3_ip::multicast_forwarding::{
164        ForwardMulticastRouteError, MulticastForwardingDisabledError, MulticastForwardingEvent,
165        MulticastRoute, MulticastRouteKey, MulticastRouteStats, MulticastRouteTarget,
166    };
167    pub use netstack3_ip::raw::{
168        RawIpSocketIcmpFilter, RawIpSocketIcmpFilterError, RawIpSocketId, RawIpSocketProtocol,
169        RawIpSocketSendToError, RawIpSocketsBindingsContext, RawIpSocketsBindingsTypes,
170        ReceivePacketError, WeakRawIpSocketId,
171    };
172    pub use netstack3_ip::socket::{
173        IpSockCreateAndSendError, IpSockCreationError, IpSockSendError,
174    };
175    pub use netstack3_ip::{
176        IpLayerEvent, IpRoutingBindingsTypes, ResolveRouteError, RouterAdvertisementEvent,
177    };
178}
179
180/// Types and utilities for dealing with neighbors.
181pub mod neighbor {
182    // Re-exported types.
183    pub use netstack3_ip::nud::{
184        Event, EventDynamicState, EventKind, EventState, LinkResolutionContext,
185        LinkResolutionNotifier, LinkResolutionResult, MAX_ENTRIES, NeighborRemovalError,
186        NudUserConfig, NudUserConfigUpdate, StaticNeighborInsertionError,
187    };
188}
189
190/// Types and utilities for dealing with routes.
191pub mod routes {
192    // Re-exported types.
193    pub use netstack3_base::{Marks, WrapBroadcastMarker};
194    pub use netstack3_ip::{
195        AddRouteError, AddableEntry, AddableEntryEither, AddableMetric, Entry, EntryEither,
196        Generation, Metric, NextHop, RawMetric, ResolvedRoute, RoutableIpAddr, RouteResolveOptions,
197        RoutingTableCookie, RoutingTableId, Rule, RuleAction, RuleMatcher, TrafficOriginMatcher,
198    };
199}
200
201/// Common types for dealing with sockets.
202pub mod socket {
203    pub use netstack3_datagram::{
204        ConnInfo, ConnectError, ExpectedConnError, ExpectedUnboundError, ListenerInfo,
205        MulticastInterfaceSelector, MulticastMembershipInterfaceSelector, SendError, SendToError,
206        SetMulticastMembershipError, SocketInfo,
207    };
208
209    pub use netstack3_base::socket::{
210        AddrIsMappedError, NotDualStackCapableError, ReusePortOption, SetDualStackEnabledError,
211        SharingDomain, ShutdownType, SocketCookie, SocketWritableListener, StrictlyZonedAddr,
212    };
213
214    pub use netstack3_base::{
215        IpSocketMatcher, SocketCookieMatcher, SocketTransportProtocolMatcher, TcpSocketMatcher,
216        TcpStateMatcher, UdpSocketMatcher, UdpStateMatcher,
217    };
218}
219
220/// Useful synchronization primitives.
221pub mod sync {
222    // We take all of our dependencies directly from base for symmetry with the
223    // other crates. However, we want to explicitly have all the dependencies in
224    // GN so we can assert the dependencies on the crate variants. This defeats
225    // rustc's unused dependency check.
226    use netstack3_sync as _;
227
228    pub use netstack3_base::sync::{
229        DebugReferences, DynDebugReferences, LockGuard, MapRcNotifier, Mutex, PrimaryRc,
230        RcNotifier, ResourceToken, ResourceTokenValue, RwLock, RwLockReadGuard, RwLockWriteGuard,
231        StrongRc, WeakRc,
232    };
233    pub use netstack3_base::{RemoveResourceResult, RemoveResourceResultWithContext};
234}
235
236/// Methods for dealing with TCP sockets.
237pub mod tcp {
238    pub use netstack3_base::{FragmentedPayload, Payload, PayloadLen, TcpSocketState};
239    pub use netstack3_tcp::{
240        AcceptError, BindError, BoundInfo, Buffer, BufferLimits, BufferSizes, ConnectError,
241        ConnectionError, ConnectionInfo, DEFAULT_FIN_WAIT2_TIMEOUT, IntoBuffers, ListenError,
242        ListenerNotifier, NoConnection, OriginalDestinationError, ReceiveBuffer, SendBuffer,
243        SetDeviceError, SetReuseAddrError, SocketAddr, SocketInfo, SocketOptions, TcpBindingsTypes,
244        TcpSettings, TcpSocketDiagnosticTuple, TcpSocketDiagnostics, TcpSocketId, UnboundInfo,
245    };
246}
247
248/// Tracing utilities.
249pub mod trace {
250    // Re-export all of the trace crate to match how the rest of core works.
251    pub use netstack3_trace::*;
252}
253
254/// Miscellaneous and common types.
255pub mod types {
256    pub use netstack3_base::{BufferSizeSettings, Counter, PositiveIsize, WorkQueueReport};
257}
258
259/// Methods for dealing with UDP sockets.
260pub mod udp {
261    pub use netstack3_udp::{
262        ReceiveUdpError, SendError, SendToError, UdpBindingsTypes, UdpPacketMeta,
263        UdpReceiveBindingsContext, UdpRemotePort, UdpSettings, UdpSocketDiagnosticTuple,
264        UdpSocketDiagnostics, UdpSocketId,
265    };
266}
267
268pub use api::CoreApi;
269pub use context::{CoreCtx, UnlockedCoreCtx};
270pub use inspect::Inspector;
271pub use marker::{BindingsContext, BindingsTypes, CoreContext, IpBindingsContext, IpExt};
272pub use netstack3_base::{
273    CtxPair, DeferredResourceRemovalContext, EventContext, InstantBindingsTypes, InstantContext,
274    MapDerefExt, MatcherBindingsTypes, ReferenceNotifiers, RngContext, SettingsContext,
275    TimerBindingsTypes, TimerContext, TxMetadata, TxMetadataBindingsTypes,
276};
277pub use state::{StackState, StackStateBuilder};
278pub use time::{AtomicInstant, Instant, TimerId};
279pub use transport::CoreTxMetadata;
280
281// Re-export useful macros.
282pub use netstack3_device::for_any_device_id;
283pub use netstack3_macros::context_ip_bounds;
284
285// Rust compiler spinning workaround (https://fxbug.dev/395694598):
286//
287// If you find yourself with a spinning rustc because you're changing traits
288// that need to be implemented by bindings, uncomment the lines below and give
289// it a go. See attached bug for details.
290//
291// unsafe impl<BT: BindingsTypes> Send for TimerId<BT> {}
292// unsafe impl<BT: BindingsTypes> Sync for TimerId<BT> {}
293// unsafe impl<BT: BindingsTypes> Send for TxMetadata<BT> {}
294// unsafe impl<BT: BindingsTypes> Sync for TxMetadata<BT> {}