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/// The device layer.
36pub mod device {
37    #[path = "."]
38    pub(crate) mod integration {
39        mod base;
40        mod blackhole;
41        mod ethernet;
42        mod loopback;
43        mod pure_ip;
44        mod socket;
45
46        pub(crate) use base::{
47            device_state, device_state_and_core_ctx, get_mtu, ip_device_state,
48            ip_device_state_and_core_ctx,
49        };
50    }
51
52    // Re-exported types.
53    pub use netstack3_base::{
54        BoundInterfaceMatcher, DeviceIdentifier, InterfaceMatcher, InterfaceProperties,
55        StrongDeviceIdentifier,
56    };
57    pub use netstack3_device::blackhole::{BlackholeDevice, BlackholeDeviceId};
58    pub use netstack3_device::ethernet::{
59        EthernetCreationProperties, EthernetDeviceEvent, EthernetDeviceId, EthernetLinkDevice,
60        EthernetWeakDeviceId, MaxEthernetFrameSize, RecvEthernetFrameMeta,
61    };
62    pub use netstack3_device::loopback::{
63        LoopbackCreationProperties, LoopbackDevice, LoopbackDeviceId, LoopbackWeakDeviceId,
64    };
65    pub use netstack3_device::pure_ip::{
66        PureIpDevice, PureIpDeviceCreationProperties, PureIpDeviceId,
67        PureIpDeviceReceiveFrameMetadata, PureIpHeaderParams, PureIpWeakDeviceId,
68    };
69    pub use netstack3_device::queue::{
70        BatchSize, ReceiveQueueBindingsContext, TransmitQueueBindingsContext,
71        TransmitQueueConfiguration, TxBufferAllocator,
72    };
73    pub use netstack3_device::{
74        ArpConfiguration, ArpConfigurationUpdate, BufferSlice, DeviceBufferBindingsTypes,
75        DeviceClassMatcher, DeviceConfiguration, DeviceConfigurationUpdate,
76        DeviceConfigurationUpdateError, DeviceCounters, DeviceId, DeviceIdAndNameMatcher,
77        DeviceLayerEventDispatcher, DeviceLayerStateTypes, DeviceProvider, DeviceSendFrameError,
78        GroBufferDestination, GroBufferStorage, GroFrameType, GroInputItem, GroIter,
79        GroOutputBuffers, GroOutputItem, MaybeContiguousBuffer, NdpConfiguration,
80        NdpConfigurationUpdate, WeakDeviceId,
81    };
82}
83
84/// Device socket API.
85pub mod device_socket {
86    pub use netstack3_base::{FrameDestination, PacketType, SendFrameErrorReason};
87    pub use netstack3_device::socket::{
88        DeviceSocketBindingsContext, DeviceSocketMetadata, DeviceSocketTypes, EthernetFrame,
89        EthernetHeaderParams, Frame, IpFrame, Protocol, ReceiveFrameError, ReceivedFrame,
90        SentFrame, SocketId, SocketInfo, TargetDevice, WeakDeviceSocketId,
91    };
92}
93
94/// Generic netstack errors.
95pub mod error {
96    pub use netstack3_base::{
97        AddressResolutionFailed, ExistsError, LocalAddressError, NotFoundError, NotSupportedError,
98        RemoteAddressError, SocketError, ZonedAddressError,
99    };
100}
101
102/// Framework for packet filtering.
103pub mod filter {
104    mod integration;
105
106    pub use netstack3_filter::{
107        Action, BindingsPacketMatcher, EitherIpProto, FilterApi, FilterBindingsContext,
108        FilterBindingsTypes, FilterIpExt, FilterIpPacket, FilterPacketMetadata, FrameDestination,
109        Hook, Interfaces, IpPacket, IpRoutines, LocalFrameDestination, MarkAction, NatRoutines,
110        PacketMatcher, PacketType, ProofOfEgressCheck, RejectType, Routine, Routines, Rule,
111        SocketEgressFilterResult, SocketInfo, SocketIngressFilterResult, SocketOpsFilter,
112        SocketOpsFilterBindingContext, TransparentProxy, TransportProtocolMatcher, Tuple,
113        UninstalledRoutine, ValidationError,
114    };
115}
116
117/// Facilities for inspecting stack state for debugging.
118pub mod inspect {
119    pub use netstack3_base::{
120        Inspectable, InspectableValue, Inspector, InspectorDeviceExt, InspectorExt,
121    };
122}
123
124/// Methods for dealing with ICMP sockets.
125pub mod icmp {
126    pub use netstack3_icmp_echo::{
127        IcmpEchoBindingsContext, IcmpEchoBindingsTypes, IcmpSocketId, ReceiveIcmpEchoError,
128    };
129}
130
131/// The Internet Protocol, versions 4 and 6.
132pub mod ip {
133    #[path = "."]
134    pub(crate) mod integration {
135        mod base;
136        mod device;
137        mod multicast_forwarding;
138        mod raw;
139
140        pub(crate) use device::CoreCtxWithIpDeviceConfiguration;
141    }
142
143    // Re-exported types.
144    pub use netstack3_base::{
145        AddressMatcher, AddressMatcherEither, AddressMatcherType, BoundAddressMatcherEither,
146        BoundPortMatcher, Mark, MarkDomain, MarkInDomainMatcher, MarkMatcher, MarkMatchers, Marks,
147        PortMatcher, SubnetMatcher, WrapBroadcastMarker,
148    };
149    pub use netstack3_ip::device::{
150        AddIpAddrSubnetError, AddrSubnetAndManualConfigEither, AddressRemovedReason,
151        CommonAddressConfig, CommonAddressProperties, IidGenerationConfiguration, IidSecret,
152        IpAddressState, IpDeviceConfiguration, IpDeviceConfigurationAndFlags,
153        IpDeviceConfigurationUpdate, IpDeviceEvent, IpDeviceIpExt, Ipv4AddrConfig,
154        Ipv4DeviceConfiguration, Ipv4DeviceConfigurationUpdate, Ipv6AddrManualConfig,
155        Ipv6DeviceConfiguration, Ipv6DeviceConfigurationUpdate, Lifetime,
156        PendingIpDeviceConfigurationUpdate, PreferredLifetime, RouteDiscoveryConfiguration,
157        RouteDiscoveryConfigurationUpdate, SetIpAddressPropertiesError, SlaacConfiguration,
158        SlaacConfigurationUpdate, StableSlaacAddressConfiguration,
159        TemporarySlaacAddressConfiguration, UpdateIpConfigurationError,
160    };
161    pub use netstack3_ip::gmp::{IgmpConfigMode, MldConfigMode};
162    pub use netstack3_ip::multicast_forwarding::{
163        ForwardMulticastRouteError, MulticastForwardingDisabledError, MulticastForwardingEvent,
164        MulticastRoute, MulticastRouteKey, MulticastRouteStats, MulticastRouteTarget,
165    };
166    pub use netstack3_ip::raw::{
167        RawIpSocketIcmpFilter, RawIpSocketIcmpFilterError, RawIpSocketId, RawIpSocketProtocol,
168        RawIpSocketSendToError, RawIpSocketsBindingsContext, RawIpSocketsBindingsTypes,
169        ReceivePacketError, WeakRawIpSocketId,
170    };
171    pub use netstack3_ip::socket::{
172        IpSockCreateAndSendError, IpSockCreationError, IpSockSendError,
173    };
174    pub use netstack3_ip::{
175        DeviceIpLayerMetadata, IpLayerEvent, IpRoutingBindingsTypes, MarksBindingsContext,
176        ResolveRouteError, RouterAdvertisementEvent, SocketMetadata,
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        TriggerNeighborProbeError,
188    };
189}
190
191/// Types and utilities for dealing with routes.
192pub mod routes {
193    // Re-exported types.
194    pub use netstack3_base::{Marks, WrapBroadcastMarker};
195    pub use netstack3_ip::{
196        AddRouteError, AddableEntry, AddableEntryEither, AddableMetric, Entry, EntryEither,
197        Generation, Metric, NextHop, RawMetric, ResolvedRoute, RoutableIpAddr, RoutePreference,
198        RouteResolveOptions, RoutingTableCookie, RoutingTableId, Rule, RuleAction, RuleMatcher,
199        TrafficOriginMatcher,
200    };
201}
202
203/// Common types for dealing with sockets.
204pub mod socket {
205    pub use netstack3_datagram::{
206        ConnInfo, ConnectError, ExpectedConnError, ExpectedUnboundError, ListenerInfo,
207        MulticastInterfaceSelector, MulticastMembershipInterfaceSelector,
208        PendingDatagramSocketError, SendError, SendToError, SetMulticastMembershipError,
209        SocketInfo,
210    };
211
212    pub use netstack3_base::socket::{
213        AddrIsMappedError, NotDualStackCapableError, ReusePortOption, SendBufferFullError,
214        SendBufferSpace, SendBufferTracking, SetDualStackEnabledError, SharingDomain, ShutdownType,
215        SocketCookie, SocketWritableListener, StrictlyZonedAddr,
216    };
217
218    pub use netstack3_base::{
219        IpSocketMatcher, SocketCookieMatcher, SocketTransportProtocolMatcher, TcpSocketMatcher,
220        TcpStateMatcher, UdpSocketMatcher, UdpStateMatcher,
221    };
222}
223
224/// Useful synchronization primitives.
225pub mod sync {
226    // We take all of our dependencies directly from base for symmetry with the
227    // other crates. However, we want to explicitly have all the dependencies in
228    // GN so we can assert the dependencies on the crate variants. This defeats
229    // rustc's unused dependency check.
230    use netstack3_sync as _;
231
232    pub use netstack3_base::sync::{
233        DebugReferences, DynDebugReferences, LockGuard, MapRcNotifier, Mutex, PrimaryRc,
234        RcNotifier, ResourceToken, ResourceTokenValue, RwLock, RwLockReadGuard, RwLockWriteGuard,
235        StrongRc, WeakRc,
236    };
237    pub use netstack3_base::{RemoveResourceResult, RemoveResourceResultWithContext};
238}
239
240/// Methods for dealing with TCP sockets.
241pub mod tcp {
242    pub use netstack3_base::{FragmentedPayload, Payload, PayloadLen, TcpSocketState};
243    pub use netstack3_tcp::{
244        AcceptError, AcceptQueueChange, BindError, BoundInfo, Buffer, BufferLimits, BufferSizes,
245        CongestionControlState, ConnectError, ConnectionError, ConnectionInfo,
246        DEFAULT_FIN_WAIT2_TIMEOUT, IntoBuffers, ListenError, ListenerNotifier, NoConnection,
247        OriginalDestinationError, ReceiveBuffer, SendBuffer, SetDeviceError, SetReuseAddrError,
248        SocketAddr, SocketInfo, SocketOptions, TcpBindingsTypes, TcpSettings,
249        TcpSocketDestructionContext, TcpSocketDiagnosticTuple, TcpSocketDiagnostics, TcpSocketId,
250        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, UdpSocketDiagnosticTuple, UdpSocketDiagnostics,
270        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    ChecksumOffloadResult, ChecksumOffloadSpec, ChecksumRxOffloading, CtxPair,
280    DeferredResourceRemovalContext, EventContext, GsoInfo, InstantBindingsTypes, InstantContext,
281    Ipv4IdMode, MapDerefExt, MatcherBindingsTypes, NetworkParsingContext,
282    NetworkSerializationContext, ProtocolSpecificOffloadSpec, ReferenceNotifiers, RngContext,
283    SettingsContext, SocketDiagnosticsSeed, TimerBindingsTypes, TimerContext, TxMetadata,
284    TxMetadataBindingsTypes,
285};
286pub use netstack3_datagram::PendingDatagramSocketError;
287pub use state::{StackState, StackStateBuilder};
288pub use time::{AtomicInstant, Instant, TimerId};
289pub use transport::CoreTxMetadata;
290
291// Re-export useful macros.
292pub use netstack3_device::for_any_device_id;
293pub use netstack3_macros::context_ip_bounds;
294
295// Rust compiler spinning workaround (https://fxbug.dev/395694598):
296//
297// If you find yourself with a spinning rustc because you're changing traits
298// that need to be implemented by bindings, uncomment the lines below and give
299// it a go. See attached bug for details.
300//
301// unsafe impl<BT: BindingsTypes> Send for TimerId<BT> {}
302// unsafe impl<BT: BindingsTypes> Sync for TimerId<BT> {}
303// unsafe impl<BT: BindingsTypes> Send for TxMetadata<BT> {}
304// unsafe impl<BT: BindingsTypes> Sync for TxMetadata<BT> {}