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