Skip to main content

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, FilterIpPacket, FilterPacketMetadata, Hook, Interfaces, IpPacket, IpRoutines,
112        MarkAction, NatRoutines, PacketMatcher, ProofOfEgressCheck, RejectType, Routine, Routines,
113        Rule, SocketEgressFilterResult, SocketIngressFilterResult, SocketOpsFilter,
114        SocketOpsFilterBindingContext, TransparentProxy, TransportProtocolMatcher, Tuple,
115        UninstalledRoutine, ValidationError,
116    };
117}
118
119/// Facilities for inspecting stack state for debugging.
120pub mod inspect {
121    pub use netstack3_base::{
122        Inspectable, InspectableValue, Inspector, InspectorDeviceExt, InspectorExt,
123    };
124}
125
126/// Methods for dealing with ICMP sockets.
127pub mod icmp {
128    pub use netstack3_icmp_echo::{
129        IcmpEchoBindingsContext, IcmpEchoBindingsTypes, IcmpEchoSettings, IcmpSocketId,
130        ReceiveIcmpEchoError,
131    };
132}
133
134/// The Internet Protocol, versions 4 and 6.
135pub mod ip {
136    #[path = "."]
137    pub(crate) mod integration {
138        mod base;
139        mod device;
140        mod multicast_forwarding;
141        mod raw;
142
143        pub(crate) use device::CoreCtxWithIpDeviceConfiguration;
144    }
145
146    // Re-exported types.
147    pub use netstack3_base::{
148        AddressMatcher, AddressMatcherEither, AddressMatcherType, BoundAddressMatcherEither,
149        BoundPortMatcher, Mark, MarkDomain, MarkInDomainMatcher, MarkMatcher, MarkMatchers, Marks,
150        PortMatcher, SubnetMatcher, WrapBroadcastMarker,
151    };
152    pub use netstack3_ip::device::{
153        AddIpAddrSubnetError, AddrSubnetAndManualConfigEither, AddressRemovedReason,
154        CommonAddressConfig, CommonAddressProperties, IidGenerationConfiguration, IidSecret,
155        IpAddressState, IpDeviceConfiguration, IpDeviceConfigurationAndFlags,
156        IpDeviceConfigurationUpdate, IpDeviceEvent, IpDeviceIpExt, Ipv4AddrConfig,
157        Ipv4DeviceConfiguration, Ipv4DeviceConfigurationUpdate, Ipv6AddrManualConfig,
158        Ipv6DeviceConfiguration, Ipv6DeviceConfigurationUpdate, Lifetime,
159        PendingIpDeviceConfigurationUpdate, PreferredLifetime, RouteDiscoveryConfiguration,
160        RouteDiscoveryConfigurationUpdate, SetIpAddressPropertiesError, SlaacConfiguration,
161        SlaacConfigurationUpdate, StableSlaacAddressConfiguration,
162        TemporarySlaacAddressConfiguration, UpdateIpConfigurationError,
163    };
164    pub use netstack3_ip::gmp::{IgmpConfigMode, MldConfigMode};
165    pub use netstack3_ip::multicast_forwarding::{
166        ForwardMulticastRouteError, MulticastForwardingDisabledError, MulticastForwardingEvent,
167        MulticastRoute, MulticastRouteKey, MulticastRouteStats, MulticastRouteTarget,
168    };
169    pub use netstack3_ip::raw::{
170        RawIpSocketIcmpFilter, RawIpSocketIcmpFilterError, RawIpSocketId, RawIpSocketProtocol,
171        RawIpSocketSendToError, RawIpSocketsBindingsContext, RawIpSocketsBindingsTypes,
172        ReceivePacketError, WeakRawIpSocketId,
173    };
174    pub use netstack3_ip::socket::{
175        IpSockCreateAndSendError, IpSockCreationError, IpSockSendError,
176    };
177    pub use netstack3_ip::{
178        IpLayerEvent, IpRoutingBindingsTypes, MarksBindingsContext, ResolveRouteError,
179        RouterAdvertisementEvent, SocketMetadata,
180    };
181}
182
183/// Types and utilities for dealing with neighbors.
184pub mod neighbor {
185    // Re-exported types.
186    pub use netstack3_ip::nud::{
187        Event, EventDynamicState, EventKind, EventState, LinkResolutionContext,
188        LinkResolutionNotifier, LinkResolutionResult, MAX_ENTRIES, NeighborRemovalError,
189        NudUserConfig, NudUserConfigUpdate, StaticNeighborInsertionError,
190        TriggerNeighborProbeError,
191    };
192}
193
194/// Types and utilities for dealing with routes.
195pub mod routes {
196    // Re-exported types.
197    pub use netstack3_base::{Marks, WrapBroadcastMarker};
198    pub use netstack3_ip::{
199        AddRouteError, AddableEntry, AddableEntryEither, AddableMetric, Entry, EntryEither,
200        Generation, Metric, NextHop, RawMetric, ResolvedRoute, RoutableIpAddr, RoutePreference,
201        RouteResolveOptions, RoutingTableCookie, RoutingTableId, Rule, RuleAction, RuleMatcher,
202        TrafficOriginMatcher,
203    };
204}
205
206/// Common types for dealing with sockets.
207pub mod socket {
208    pub use netstack3_datagram::{
209        ConnInfo, ConnectError, ExpectedConnError, ExpectedUnboundError, ListenerInfo,
210        MulticastInterfaceSelector, MulticastMembershipInterfaceSelector, SendError, SendToError,
211        SetMulticastMembershipError, SocketInfo,
212    };
213
214    pub use netstack3_base::socket::{
215        AddrIsMappedError, NotDualStackCapableError, ReusePortOption, SetDualStackEnabledError,
216        SharingDomain, ShutdownType, SocketCookie, SocketWritableListener, StrictlyZonedAddr,
217    };
218
219    pub use netstack3_base::{
220        IpSocketMatcher, SocketCookieMatcher, SocketTransportProtocolMatcher, TcpSocketMatcher,
221        TcpStateMatcher, UdpSocketMatcher, UdpStateMatcher,
222    };
223}
224
225/// Useful synchronization primitives.
226pub mod sync {
227    // We take all of our dependencies directly from base for symmetry with the
228    // other crates. However, we want to explicitly have all the dependencies in
229    // GN so we can assert the dependencies on the crate variants. This defeats
230    // rustc's unused dependency check.
231    use netstack3_sync as _;
232
233    pub use netstack3_base::sync::{
234        DebugReferences, DynDebugReferences, LockGuard, MapRcNotifier, Mutex, PrimaryRc,
235        RcNotifier, ResourceToken, ResourceTokenValue, RwLock, RwLockReadGuard, RwLockWriteGuard,
236        StrongRc, WeakRc,
237    };
238    pub use netstack3_base::{RemoveResourceResult, RemoveResourceResultWithContext};
239}
240
241/// Methods for dealing with TCP sockets.
242pub mod tcp {
243    pub use netstack3_base::{FragmentedPayload, Payload, PayloadLen, TcpSocketState};
244    pub use netstack3_tcp::{
245        AcceptError, BindError, BoundInfo, Buffer, BufferLimits, BufferSizes,
246        CongestionControlState, ConnectError, ConnectionError, ConnectionInfo,
247        DEFAULT_FIN_WAIT2_TIMEOUT, IntoBuffers, ListenError, ListenerNotifier, NoConnection,
248        OriginalDestinationError, ReceiveBuffer, SendBuffer, SetDeviceError, SetReuseAddrError,
249        SocketAddr, SocketInfo, SocketOptions, TcpBindingsTypes, TcpSettings,
250        TcpSocketDiagnosticTuple, TcpSocketDiagnostics, TcpSocketId, TcpSocketInfo, UnboundInfo,
251    };
252}
253
254/// Tracing utilities.
255pub mod trace {
256    // Re-export all of the trace crate to match how the rest of core works.
257    pub use netstack3_trace::*;
258}
259
260/// Miscellaneous and common types.
261pub mod types {
262    pub use netstack3_base::{BufferSizeSettings, Counter, PositiveIsize, WorkQueueReport};
263}
264
265/// Methods for dealing with UDP sockets.
266pub mod udp {
267    pub use netstack3_udp::{
268        ReceiveUdpError, SendError, SendToError, UdpBindingsTypes, UdpPacketMeta,
269        UdpReceiveBindingsContext, UdpRemotePort, UdpSettings, UdpSocketDiagnosticTuple,
270        UdpSocketDiagnostics, UdpSocketId,
271    };
272}
273
274pub use api::CoreApi;
275pub use context::{CoreCtx, UnlockedCoreCtx};
276pub use inspect::Inspector;
277pub use marker::{BindingsContext, BindingsTypes, CoreContext, IpBindingsContext, IpExt};
278pub use netstack3_base::{
279    CtxPair, DeferredResourceRemovalContext, EventContext, InstantBindingsTypes, InstantContext,
280    MapDerefExt, MatcherBindingsTypes, ReferenceNotifiers, RngContext, SettingsContext,
281    TimerBindingsTypes, TimerContext, TxMetadata, TxMetadataBindingsTypes,
282};
283pub use state::{StackState, StackStateBuilder};
284pub use time::{AtomicInstant, Instant, TimerId};
285pub use transport::CoreTxMetadata;
286
287// Re-export useful macros.
288pub use netstack3_device::for_any_device_id;
289pub use netstack3_macros::context_ip_bounds;
290
291// Rust compiler spinning workaround (https://fxbug.dev/395694598):
292//
293// If you find yourself with a spinning rustc because you're changing traits
294// that need to be implemented by bindings, uncomment the lines below and give
295// it a go. See attached bug for details.
296//
297// unsafe impl<BT: BindingsTypes> Send for TimerId<BT> {}
298// unsafe impl<BT: BindingsTypes> Sync for TimerId<BT> {}
299// unsafe impl<BT: BindingsTypes> Send for TxMetadata<BT> {}
300// unsafe impl<BT: BindingsTypes> Sync for TxMetadata<BT> {}