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