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