1use alloc::vec::Vec;
8use core::borrow::Borrow;
9use core::convert::Infallible as Never;
10use core::fmt::Debug;
11use core::marker::PhantomData;
12use core::num::{NonZeroU8, NonZeroU16};
13use core::ops::ControlFlow;
14use lock_order::lock::{DelegatedOrderedLockAccess, OrderedLockAccess, OrderedLockRef};
15
16use derivative::Derivative;
17use either::Either;
18use log::{debug, trace};
19use net_types::ip::{GenericOverIp, Ip, IpVersionMarker};
20use net_types::{SpecifiedAddr, Witness as _, ZonedAddr};
21use netstack3_base::socket::{
22 self, AddrIsMappedError, AddrVec, AddrVecIter, ConnAddr, ConnInfoAddr, ConnIpAddr,
23 IncompatibleError, InsertError, ListenerAddrInfo, MaybeDualStack, ShutdownType, SocketCookie,
24 SocketIpAddr, SocketMapAddrSpec, SocketMapAddrStateSpec, SocketMapConflictPolicy,
25 SocketMapStateSpec, SocketWritableListener,
26};
27use netstack3_base::socketmap::{IterShadows as _, SocketMap};
28use netstack3_base::sync::{RwLock, StrongRc};
29use netstack3_base::{
30 AnyDevice, ContextPair, CoreTxMetadataContext, CounterContext, DeviceIdContext, IcmpIpExt,
31 Inspector, InspectorDeviceExt, LocalAddressError, Mark, MarkDomain, Marks, PortAllocImpl,
32 ReferenceNotifiers, RemoveResourceResultWithContext, RngContext, SettingsContext, SocketError,
33 StrongDeviceIdentifier, UninstantiableWrapper, WeakDeviceIdentifier,
34};
35use netstack3_datagram::{
36 self as datagram, DatagramApi, DatagramBindingsTypes, DatagramFlowId, DatagramSocketMapSpec,
37 DatagramSocketSet, DatagramSocketSpec, DatagramSpecBoundStateContext, DatagramSpecStateContext,
38 DatagramStateContext, ExpectedUnboundError, NonDualStackConverter,
39 NonDualStackDatagramSpecBoundStateContext,
40};
41use netstack3_ip::icmp::{EchoTransportContextMarker, IcmpRxCounters};
42use netstack3_ip::socket::SocketHopLimits;
43use netstack3_ip::{
44 IpHeaderInfo, IpLayerIpExt, IpTransportContext, LocalDeliveryPacketInfo,
45 MulticastMembershipHandler, ReceiveIpPacketMeta, SocketMetadata, TransportIpContext,
46};
47use packet::{BufferMut, NestablePacketBuilder as _, ParsablePacket as _, ParseBuffer};
48use packet_formats::icmp::{IcmpEchoReply, IcmpEchoRequest, IcmpPacketBuilder, IcmpPacketRaw};
49use packet_formats::ip::{IpProtoExt, Ipv4Proto, Ipv6Proto};
50
51use crate::internal::settings::IcmpEchoSettings;
52
53pub trait IpExt: datagram::IpExt + IcmpIpExt + IpLayerIpExt {}
55impl<O: datagram::IpExt + IcmpIpExt + IpLayerIpExt> IpExt for O {}
56
57#[derive(Derivative, GenericOverIp)]
59#[derivative(Default(bound = ""))]
60#[generic_over_ip(I, Ip)]
61pub struct IcmpSockets<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> {
62 bound_and_id_allocator: RwLock<BoundSockets<I, D, BT>>,
63 all_sockets: RwLock<IcmpSocketSet<I, D, BT>>,
66}
67
68impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>
69 OrderedLockAccess<BoundSockets<I, D, BT>> for IcmpSockets<I, D, BT>
70{
71 type Lock = RwLock<BoundSockets<I, D, BT>>;
72 fn ordered_lock_access(&self) -> OrderedLockRef<'_, Self::Lock> {
73 OrderedLockRef::new(&self.bound_and_id_allocator)
74 }
75}
76
77impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>
78 OrderedLockAccess<IcmpSocketSet<I, D, BT>> for IcmpSockets<I, D, BT>
79{
80 type Lock = RwLock<IcmpSocketSet<I, D, BT>>;
81 fn ordered_lock_access(&self) -> OrderedLockRef<'_, Self::Lock> {
82 OrderedLockRef::new(&self.all_sockets)
83 }
84}
85
86#[derive(GenericOverIp, Derivative)]
88#[derivative(Eq(bound = ""), PartialEq(bound = ""), Hash(bound = ""))]
89#[generic_over_ip(I, Ip)]
90pub struct IcmpSocketId<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>(
91 datagram::StrongRc<I, D, Icmp<BT>>,
92);
93
94impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> IcmpSocketId<I, D, BT> {
95 pub fn socket_cookie(&self) -> SocketCookie {
97 let Self(inner) = self;
98 SocketCookie::new(inner.resource_token())
99 }
100
101 pub fn socket_info(&self) -> netstack3_base::socket::SocketInfo {
103 netstack3_base::socket::SocketInfo {
104 proto: I::map_ip(
105 (),
106 |()| netstack3_base::socket::EitherIpProto::V4(Ipv4Proto::Icmp),
107 |()| netstack3_base::socket::EitherIpProto::V6(Ipv6Proto::Icmpv6),
108 ),
109 cookie: self.socket_cookie(),
110 }
111 }
112}
113
114impl<CC, I, BT> SocketMetadata<CC> for IcmpSocketId<I, CC::WeakDeviceId, BT>
115where
116 CC: IcmpEchoStateContext<I, BT>,
117 I: IpExt,
118 BT: IcmpEchoBindingsTypes,
119{
120 fn socket_info(&self, _core_ctx: &mut CC) -> netstack3_base::socket::SocketInfo {
121 self.socket_info()
122 }
123
124 fn marks(&self, core_ctx: &mut CC) -> Marks {
125 core_ctx.with_socket_state(self, |_core_ctx, state| state.options().marks().clone())
126 }
127}
128
129impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> Clone
130 for IcmpSocketId<I, D, BT>
131{
132 #[cfg_attr(feature = "instrumented", track_caller)]
133 fn clone(&self) -> Self {
134 let Self(rc) = self;
135 Self(StrongRc::clone(rc))
136 }
137}
138
139impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>
140 From<datagram::StrongRc<I, D, Icmp<BT>>> for IcmpSocketId<I, D, BT>
141{
142 fn from(value: datagram::StrongRc<I, D, Icmp<BT>>) -> Self {
143 Self(value)
144 }
145}
146
147impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>
148 Borrow<datagram::StrongRc<I, D, Icmp<BT>>> for IcmpSocketId<I, D, BT>
149{
150 fn borrow(&self) -> &datagram::StrongRc<I, D, Icmp<BT>> {
151 let Self(rc) = self;
152 rc
153 }
154}
155
156impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>
157 PartialEq<WeakIcmpSocketId<I, D, BT>> for IcmpSocketId<I, D, BT>
158{
159 fn eq(&self, other: &WeakIcmpSocketId<I, D, BT>) -> bool {
160 let Self(rc) = self;
161 let WeakIcmpSocketId(weak) = other;
162 StrongRc::weak_ptr_eq(rc, weak)
163 }
164}
165
166impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> Debug
167 for IcmpSocketId<I, D, BT>
168{
169 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
170 let Self(rc) = self;
171 f.debug_tuple("IcmpSocketId").field(&StrongRc::debug_id(rc)).finish()
172 }
173}
174
175impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> IcmpSocketId<I, D, BT> {
176 #[cfg(any(test, feature = "testutils"))]
179 pub fn state(&self) -> &RwLock<IcmpSocketState<I, D, BT>> {
180 let Self(rc) = self;
181 rc.state()
182 }
183
184 pub fn debug_references(&self) -> impl Debug {
186 let Self(rc) = self;
187 StrongRc::debug_references(rc)
188 }
189
190 pub fn downgrade(&self) -> WeakIcmpSocketId<I, D, BT> {
192 let Self(rc) = self;
193 WeakIcmpSocketId(StrongRc::downgrade(rc))
194 }
195
196 pub fn external_data(&self) -> &BT::ExternalData<I> {
198 let Self(rc) = self;
199 rc.external_data()
200 }
201}
202
203impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>
204 DelegatedOrderedLockAccess<IcmpSocketState<I, D, BT>> for IcmpSocketId<I, D, BT>
205{
206 type Inner = datagram::ReferenceState<I, D, Icmp<BT>>;
207 fn delegate_ordered_lock_access(&self) -> &Self::Inner {
208 let Self(rc) = self;
209 &*rc
210 }
211}
212
213#[derive(GenericOverIp, Derivative)]
215#[derivative(Eq(bound = ""), PartialEq(bound = ""), Hash(bound = ""), Clone(bound = ""))]
216#[generic_over_ip(I, Ip)]
217pub struct WeakIcmpSocketId<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>(
218 datagram::WeakRc<I, D, Icmp<BT>>,
219);
220
221impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> PartialEq<IcmpSocketId<I, D, BT>>
222 for WeakIcmpSocketId<I, D, BT>
223{
224 fn eq(&self, other: &IcmpSocketId<I, D, BT>) -> bool {
225 PartialEq::eq(other, self)
226 }
227}
228
229impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> Debug
230 for WeakIcmpSocketId<I, D, BT>
231{
232 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
233 let Self(rc) = self;
234 f.debug_tuple("WeakIcmpSocketId").field(&rc.debug_id()).finish()
235 }
236}
237
238impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> WeakIcmpSocketId<I, D, BT> {
239 #[cfg_attr(feature = "instrumented", track_caller)]
240 pub fn upgrade(&self) -> Option<IcmpSocketId<I, D, BT>> {
241 let Self(rc) = self;
242 rc.upgrade().map(IcmpSocketId)
243 }
244}
245
246pub type IcmpSocketSet<I, D, BT> = DatagramSocketSet<I, D, Icmp<BT>>;
248pub type IcmpSocketState<I, D, BT> = datagram::SocketState<I, D, Icmp<BT>>;
250pub type IcmpSocketTxMetadata<I, D, BT> = datagram::TxMetadata<I, D, Icmp<BT>>;
252
253pub enum ReceiveIcmpEchoError {
255 QueueFull,
257}
258
259pub trait IcmpEchoBindingsContext<I: IpExt, D: StrongDeviceIdentifier>:
262 IcmpEchoBindingsTypes + ReferenceNotifiers + RngContext + SettingsContext<IcmpEchoSettings>
263{
264 fn receive_icmp_echo_reply<B: BufferMut>(
266 &mut self,
267 conn: &IcmpSocketId<I, D::Weak, Self>,
268 device_id: &D,
269 src_ip: I::Addr,
270 dst_ip: I::Addr,
271 id: u16,
272 data: B,
273 ) -> Result<(), ReceiveIcmpEchoError>;
274}
275
276pub trait IcmpEchoBindingsTypes: DatagramBindingsTypes + Sized + 'static {
289 type ExternalData<I: Ip>: Debug + Send + Sync + 'static;
291 type SocketWritableListener: SocketWritableListener + Debug + Send + Sync + 'static;
293}
294
295pub trait IcmpEchoContextMarker {}
299
300pub trait IcmpEchoBoundStateContext<I: IcmpIpExt + IpExt, BC: IcmpEchoBindingsTypes>:
302 DeviceIdContext<AnyDevice> + IcmpEchoContextMarker
303{
304 type IpSocketsCtx<'a>: TransportIpContext<I, BC>
306 + MulticastMembershipHandler<I, BC>
307 + DeviceIdContext<AnyDevice, DeviceId = Self::DeviceId, WeakDeviceId = Self::WeakDeviceId>
308 + CounterContext<IcmpRxCounters<I>>
309 + CoreTxMetadataContext<IcmpSocketTxMetadata<I, Self::WeakDeviceId, BC>, BC>;
310
311 fn with_icmp_ctx_and_sockets_mut<
314 O,
315 F: FnOnce(&mut Self::IpSocketsCtx<'_>, &mut BoundSockets<I, Self::WeakDeviceId, BC>) -> O,
316 >(
317 &mut self,
318 cb: F,
319 ) -> O;
320}
321
322pub trait IcmpEchoStateContext<I: IcmpIpExt + IpExt, BC: IcmpEchoBindingsTypes>:
324 DeviceIdContext<AnyDevice> + IcmpEchoContextMarker
325{
326 type SocketStateCtx<'a>: IcmpEchoBoundStateContext<I, BC>
328 + DeviceIdContext<AnyDevice, DeviceId = Self::DeviceId, WeakDeviceId = Self::WeakDeviceId>;
329
330 fn with_all_sockets_mut<O, F: FnOnce(&mut IcmpSocketSet<I, Self::WeakDeviceId, BC>) -> O>(
333 &mut self,
334 cb: F,
335 ) -> O;
336
337 fn with_all_sockets<O, F: FnOnce(&IcmpSocketSet<I, Self::WeakDeviceId, BC>) -> O>(
340 &mut self,
341 cb: F,
342 ) -> O;
343
344 fn with_bound_state_context<O, F: FnOnce(&mut Self::SocketStateCtx<'_>) -> O>(
346 &mut self,
347 cb: F,
348 ) -> O;
349
350 fn with_socket_state<
353 O,
354 F: FnOnce(&mut Self::SocketStateCtx<'_>, &IcmpSocketState<I, Self::WeakDeviceId, BC>) -> O,
355 >(
356 &mut self,
357 id: &IcmpSocketId<I, Self::WeakDeviceId, BC>,
358 cb: F,
359 ) -> O;
360
361 fn with_socket_state_mut<
363 O,
364 F: FnOnce(&mut Self::SocketStateCtx<'_>, &mut IcmpSocketState<I, Self::WeakDeviceId, BC>) -> O,
365 >(
366 &mut self,
367 id: &IcmpSocketId<I, Self::WeakDeviceId, BC>,
368 cb: F,
369 ) -> O;
370
371 fn for_each_socket<
373 F: FnMut(
374 &mut Self::SocketStateCtx<'_>,
375 &IcmpSocketId<I, Self::WeakDeviceId, BC>,
376 &IcmpSocketState<I, Self::WeakDeviceId, BC>,
377 ),
378 >(
379 &mut self,
380 cb: F,
381 );
382}
383
384pub struct Icmp<BT>(PhantomData<BT>, Never);
386
387impl<BT: IcmpEchoBindingsTypes> DatagramSocketSpec for Icmp<BT> {
388 const NAME: &'static str = "ICMP_ECHO";
389 type AddrSpec = IcmpAddrSpec;
390
391 type SocketId<I: datagram::IpExt, D: WeakDeviceIdentifier> = IcmpSocketId<I, D, BT>;
392 type WeakSocketId<I: datagram::IpExt, D: WeakDeviceIdentifier> = WeakIcmpSocketId<I, D, BT>;
393
394 type OtherStackIpOptions<I: datagram::IpExt, D: WeakDeviceIdentifier> = ();
395
396 type SharingState = ();
397
398 type SocketMapSpec<I: datagram::IpExt + datagram::DualStackIpExt, D: WeakDeviceIdentifier> =
399 IcmpSocketMapStateSpec<I, D, BT>;
400
401 fn ip_proto<I: IpProtoExt>() -> I::Proto {
402 I::map_ip((), |()| Ipv4Proto::Icmp, |()| Ipv6Proto::Icmpv6)
403 }
404
405 fn make_bound_socket_map_id<I: datagram::IpExt, D: WeakDeviceIdentifier>(
406 s: &Self::SocketId<I, D>,
407 ) -> <Self::SocketMapSpec<I, D> as datagram::DatagramSocketMapSpec<
408 I,
409 D,
410 Self::AddrSpec,
411 >>::BoundSocketId{
412 s.clone()
413 }
414
415 type Serializer<I: datagram::IpExt, B: BufferMut> =
416 packet::Nested<B, IcmpPacketBuilder<I, IcmpEchoRequest>>;
417 type SerializeError = packet_formats::error::ParseError;
418
419 type ExternalData<I: Ip> = BT::ExternalData<I>;
420 type Settings = IcmpEchoSettings;
421
422 type Counters<I: Ip> = ();
424 type SocketWritableListener = BT::SocketWritableListener;
425
426 const FIXED_HEADER_SIZE: usize = 0;
430
431 fn make_packet<I: datagram::IpExt, B: BufferMut>(
432 mut body: B,
433 addr: &socket::ConnIpAddr<
434 I::Addr,
435 <Self::AddrSpec as SocketMapAddrSpec>::LocalIdentifier,
436 <Self::AddrSpec as SocketMapAddrSpec>::RemoteIdentifier,
437 >,
438 ) -> Result<Self::Serializer<I, B>, Self::SerializeError> {
439 let ConnIpAddr { local: (local_ip, id), remote: (remote_ip, ()) } = addr;
440 let icmp_echo: packet_formats::icmp::IcmpPacketRaw<I, &[u8], IcmpEchoRequest> =
441 body.parse()?;
442 debug!(
443 "preparing ICMP echo request {local_ip} to {remote_ip}: id={}, seq={}",
444 id,
445 icmp_echo.message().seq()
446 );
447 let icmp_builder = IcmpPacketBuilder::<I, _>::new(
448 local_ip.addr(),
449 remote_ip.addr(),
450 packet_formats::icmp::IcmpZeroCode,
451 IcmpEchoRequest::new(id.get(), icmp_echo.message().seq()),
452 );
453 Ok(icmp_builder.wrap_body(body))
454 }
455
456 fn try_alloc_listen_identifier<I: datagram::IpExt, D: WeakDeviceIdentifier>(
457 bindings_ctx: &mut impl RngContext,
458 is_available: impl Fn(
459 <Self::AddrSpec as SocketMapAddrSpec>::LocalIdentifier,
460 ) -> Result<(), datagram::InUseError>,
461 ) -> Option<<Self::AddrSpec as SocketMapAddrSpec>::LocalIdentifier> {
462 let mut port = IcmpPortAlloc::<I, D, BT>::rand_ephemeral(&mut bindings_ctx.rng());
463 for _ in IcmpPortAlloc::<I, D, BT>::EPHEMERAL_RANGE {
464 let tryport = NonZeroU16::new(port.get()).unwrap();
467 match is_available(tryport) {
468 Ok(()) => return Some(tryport),
469 Err(datagram::InUseError {}) => port.next(),
470 }
471 }
472 None
473 }
474
475 type ListenerIpAddr<I: datagram::IpExt> = socket::ListenerIpAddr<I::Addr, NonZeroU16>;
476
477 type ConnIpAddr<I: datagram::IpExt> = ConnIpAddr<
478 I::Addr,
479 <Self::AddrSpec as SocketMapAddrSpec>::LocalIdentifier,
480 <Self::AddrSpec as SocketMapAddrSpec>::RemoteIdentifier,
481 >;
482
483 type ConnState<I: datagram::IpExt, D: WeakDeviceIdentifier> = datagram::ConnState<I, D, Self>;
484 type ConnStateExtra = u16;
488
489 fn conn_info_from_state<I: IpExt, D: WeakDeviceIdentifier>(
490 state: &Self::ConnState<I, D>,
491 ) -> datagram::ConnInfo<I::Addr, D> {
492 let ConnAddr { ip, device } = state.addr();
493 let extra = state.extra();
494 let ConnInfoAddr { local: (local_ip, local_identifier), remote: (remote_ip, ()) } =
495 ip.clone().into();
496 datagram::ConnInfo::new(local_ip, local_identifier, remote_ip, *extra, || {
497 device.clone().expect("device must be bound for addresses that require zones")
499 })
500 }
501
502 fn try_alloc_local_id<I: IpExt, D: WeakDeviceIdentifier, BC: RngContext>(
503 bound: &IcmpBoundSockets<I, D, BT>,
504 bindings_ctx: &mut BC,
505 flow: datagram::DatagramFlowId<I::Addr, ()>,
506 ) -> Option<NonZeroU16> {
507 let mut rng = bindings_ctx.rng();
508 netstack3_base::simple_randomized_port_alloc(&mut rng, &flow, &IcmpPortAlloc(bound), &())
509 .map(|p| NonZeroU16::new(p).expect("ephemeral ports should be non-zero"))
510 }
511
512 fn upgrade_socket_id<I: datagram::IpExt, D: WeakDeviceIdentifier>(
513 id: &Self::WeakSocketId<I, D>,
514 ) -> Option<Self::SocketId<I, D>> {
515 id.upgrade()
516 }
517
518 fn downgrade_socket_id<I: datagram::IpExt, D: WeakDeviceIdentifier>(
519 id: &Self::SocketId<I, D>,
520 ) -> Self::WeakSocketId<I, D> {
521 IcmpSocketId::downgrade(id)
522 }
523}
524
525pub enum IcmpAddrSpec {}
527
528impl SocketMapAddrSpec for IcmpAddrSpec {
529 type RemoteIdentifier = ();
530 type LocalIdentifier = NonZeroU16;
531}
532
533type IcmpBoundSockets<I, D, BT> = datagram::BoundDatagramSocketMap<I, D, Icmp<BT>>;
534
535struct IcmpPortAlloc<'a, I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>(
536 &'a IcmpBoundSockets<I, D, BT>,
537);
538
539impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> PortAllocImpl
540 for IcmpPortAlloc<'_, I, D, BT>
541{
542 const EPHEMERAL_RANGE: core::ops::RangeInclusive<u16> = 1..=u16::MAX;
543 type Id = DatagramFlowId<I::Addr, ()>;
544 type PortAvailableArg = ();
545
546 fn is_port_available(&self, id: &Self::Id, port: u16, (): &()) -> bool {
547 let Self(socketmap) = self;
548 let port = NonZeroU16::new(port).unwrap();
551 let conn = ConnAddr {
552 ip: ConnIpAddr { local: (id.local_ip, port), remote: (id.remote_ip, ()) },
553 device: None,
554 };
555
556 AddrVec::from(conn).iter_shadows().all(|a| match &a {
559 AddrVec::Listen(l) => socketmap.listeners().get_by_addr(&l).is_none(),
560 AddrVec::Conn(c) => socketmap.conns().get_by_addr(&c).is_none(),
561 } && socketmap.get_shadower_counts(&a) == 0)
562 }
563}
564
565#[derive(Derivative)]
567#[derivative(Default(bound = ""))]
568pub struct BoundSockets<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> {
569 pub(crate) socket_map: IcmpBoundSockets<I, D, BT>,
570}
571
572impl<I, BC, CC> NonDualStackDatagramSpecBoundStateContext<I, CC, BC> for Icmp<BC>
573where
574 I: IpExt + datagram::DualStackIpExt,
575 BC: IcmpEchoBindingsContext<I, CC::DeviceId>,
576 CC: DeviceIdContext<AnyDevice> + IcmpEchoContextMarker,
577{
578 fn nds_converter(_core_ctx: &CC) -> impl NonDualStackConverter<I, CC::WeakDeviceId, Self> {
579 ()
580 }
581}
582
583impl<I, BC, CC> DatagramSpecBoundStateContext<I, CC, BC> for Icmp<BC>
584where
585 I: IpExt + datagram::DualStackIpExt,
586 BC: IcmpEchoBindingsContext<I, CC::DeviceId>,
587 CC: IcmpEchoBoundStateContext<I, BC> + IcmpEchoContextMarker,
588{
589 type IpSocketsCtx<'a> = CC::IpSocketsCtx<'a>;
590
591 type DualStackContext = UninstantiableWrapper<CC>;
593
594 type NonDualStackContext = CC;
595
596 fn with_bound_sockets<
597 O,
598 F: FnOnce(&mut Self::IpSocketsCtx<'_>, &IcmpBoundSockets<I, CC::WeakDeviceId, BC>) -> O,
599 >(
600 core_ctx: &mut CC,
601 cb: F,
602 ) -> O {
603 IcmpEchoBoundStateContext::with_icmp_ctx_and_sockets_mut(
604 core_ctx,
605 |ctx, BoundSockets { socket_map }| cb(ctx, &socket_map),
606 )
607 }
608
609 fn with_bound_sockets_mut<
610 O,
611 F: FnOnce(&mut Self::IpSocketsCtx<'_>, &mut IcmpBoundSockets<I, CC::WeakDeviceId, BC>) -> O,
612 >(
613 core_ctx: &mut CC,
614 cb: F,
615 ) -> O {
616 IcmpEchoBoundStateContext::with_icmp_ctx_and_sockets_mut(
617 core_ctx,
618 |ctx, BoundSockets { socket_map }| cb(ctx, socket_map),
619 )
620 }
621
622 fn dual_stack_context(
623 core_ctx: &CC,
624 ) -> MaybeDualStack<&Self::DualStackContext, &Self::NonDualStackContext> {
625 MaybeDualStack::NotDualStack(core_ctx)
626 }
627
628 fn dual_stack_context_mut(
629 core_ctx: &mut CC,
630 ) -> MaybeDualStack<&mut Self::DualStackContext, &mut Self::NonDualStackContext> {
631 MaybeDualStack::NotDualStack(core_ctx)
632 }
633
634 fn with_transport_context<O, F: FnOnce(&mut Self::IpSocketsCtx<'_>) -> O>(
635 core_ctx: &mut CC,
636 cb: F,
637 ) -> O {
638 IcmpEchoBoundStateContext::with_icmp_ctx_and_sockets_mut(core_ctx, |ctx, _sockets| cb(ctx))
639 }
640}
641
642impl<I, BC, CC> DatagramSpecStateContext<I, CC, BC> for Icmp<BC>
643where
644 I: IpExt + datagram::DualStackIpExt,
645 BC: IcmpEchoBindingsContext<I, CC::DeviceId>,
646 CC: IcmpEchoStateContext<I, BC>,
647{
648 type SocketsStateCtx<'a> = CC::SocketStateCtx<'a>;
649
650 fn with_all_sockets_mut<O, F: FnOnce(&mut IcmpSocketSet<I, CC::WeakDeviceId, BC>) -> O>(
651 core_ctx: &mut CC,
652 cb: F,
653 ) -> O {
654 IcmpEchoStateContext::with_all_sockets_mut(core_ctx, cb)
655 }
656
657 fn with_all_sockets<O, F: FnOnce(&IcmpSocketSet<I, CC::WeakDeviceId, BC>) -> O>(
658 core_ctx: &mut CC,
659 cb: F,
660 ) -> O {
661 IcmpEchoStateContext::with_all_sockets(core_ctx, cb)
662 }
663
664 fn with_socket_state<
665 O,
666 F: FnOnce(&mut Self::SocketsStateCtx<'_>, &IcmpSocketState<I, CC::WeakDeviceId, BC>) -> O,
667 >(
668 core_ctx: &mut CC,
669 id: &IcmpSocketId<I, CC::WeakDeviceId, BC>,
670 cb: F,
671 ) -> O {
672 IcmpEchoStateContext::with_socket_state(core_ctx, id, cb)
673 }
674
675 fn with_socket_state_mut<
676 O,
677 F: FnOnce(&mut Self::SocketsStateCtx<'_>, &mut IcmpSocketState<I, CC::WeakDeviceId, BC>) -> O,
678 >(
679 core_ctx: &mut CC,
680 id: &IcmpSocketId<I, CC::WeakDeviceId, BC>,
681 cb: F,
682 ) -> O {
683 IcmpEchoStateContext::with_socket_state_mut(core_ctx, id, cb)
684 }
685
686 fn for_each_socket<
687 F: FnMut(
688 &mut Self::SocketsStateCtx<'_>,
689 &IcmpSocketId<I, CC::WeakDeviceId, BC>,
690 &IcmpSocketState<I, CC::WeakDeviceId, BC>,
691 ),
692 >(
693 core_ctx: &mut CC,
694 cb: F,
695 ) {
696 IcmpEchoStateContext::for_each_socket(core_ctx, cb)
697 }
698}
699
700pub struct IcmpSocketMapStateSpec<I, D, BT>(PhantomData<(I, D, BT)>, Never);
703
704impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> SocketMapStateSpec
705 for IcmpSocketMapStateSpec<I, D, BT>
706{
707 type ListenerId = IcmpSocketId<I, D, BT>;
708 type ConnId = IcmpSocketId<I, D, BT>;
709
710 type AddrVecTag = ();
711
712 type ListenerSharingState = ();
713 type ConnSharingState = ();
714
715 type ListenerAddrState = Self::ListenerId;
716
717 type ConnAddrState = Self::ConnId;
718 fn listener_tag(
719 ListenerAddrInfo { has_device: _, specified_addr: _ }: ListenerAddrInfo,
720 _state: &Self::ListenerAddrState,
721 ) -> Self::AddrVecTag {
722 ()
723 }
724 fn connected_tag(_has_device: bool, _state: &Self::ConnAddrState) -> Self::AddrVecTag {
725 ()
726 }
727}
728
729impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> SocketMapAddrStateSpec
730 for IcmpSocketId<I, D, BT>
731{
732 type Id = Self;
733
734 type SharingState = ();
735
736 type Inserter<'a>
737 = core::convert::Infallible
738 where
739 Self: 'a;
740
741 fn new(_new_sharing_state: &Self::SharingState, id: Self::Id) -> Self {
742 id
743 }
744
745 fn contains_id(&self, id: &Self::Id) -> bool {
746 self == id
747 }
748
749 fn try_get_inserter<'a, 'b>(
750 &'b mut self,
751 _new_sharing_state: &'a Self::SharingState,
752 ) -> Result<Self::Inserter<'b>, IncompatibleError> {
753 Err(IncompatibleError)
754 }
755
756 fn could_insert(
757 &self,
758 _new_sharing_state: &Self::SharingState,
759 ) -> Result<(), IncompatibleError> {
760 Err(IncompatibleError)
761 }
762
763 fn remove_by_id(&mut self, _id: Self::Id) -> socket::RemoveResult {
764 socket::RemoveResult::IsLast
765 }
766}
767
768impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>
769 DatagramSocketMapSpec<I, D, IcmpAddrSpec> for IcmpSocketMapStateSpec<I, D, BT>
770{
771 type BoundSocketId = IcmpSocketId<I, D, BT>;
772}
773
774impl<AA, I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes>
775 SocketMapConflictPolicy<AA, (), I, D, IcmpAddrSpec> for IcmpSocketMapStateSpec<I, D, BT>
776where
777 AA: Into<AddrVec<I, D, IcmpAddrSpec>> + Clone,
778{
779 fn check_insert_conflicts(
780 _new_sharing_state: &(),
781 addr: &AA,
782 socketmap: &SocketMap<AddrVec<I, D, IcmpAddrSpec>, socket::Bound<Self>>,
783 ) -> Result<(), socket::InsertError> {
784 let addr: AddrVec<_, _, _> = addr.clone().into();
785 if addr.iter_shadows().any(|a| socketmap.get(&a).is_some()) {
787 return Err(InsertError::ShadowAddrExists);
788 }
789
790 if socketmap.descendant_counts(&addr).len() != 0 {
793 return Err(InsertError::WouldShadowExisting);
794 }
795 Ok(())
796 }
797}
798
799pub struct IcmpEchoSocketApi<I: Ip, C>(C, IpVersionMarker<I>);
801
802impl<I: Ip, C> IcmpEchoSocketApi<I, C> {
803 pub fn new(ctx: C) -> Self {
805 Self(ctx, IpVersionMarker::new())
806 }
807}
808
809type IcmpApiSocketId<I, C> = IcmpSocketId<
814 I,
815 <<C as ContextPair>::CoreContext as DeviceIdContext<AnyDevice>>::WeakDeviceId,
816 <C as ContextPair>::BindingsContext,
817>;
818
819impl<I, C> IcmpEchoSocketApi<I, C>
820where
821 I: datagram::IpExt,
822 C: ContextPair,
823 C::CoreContext: IcmpEchoStateContext<I, C::BindingsContext>
824 + DatagramStateContext<I, C::BindingsContext, Icmp<C::BindingsContext>>,
827 C::BindingsContext:
828 IcmpEchoBindingsContext<I, <C::CoreContext as DeviceIdContext<AnyDevice>>::DeviceId>,
829{
830 fn core_ctx(&mut self) -> &mut C::CoreContext {
831 let Self(pair, IpVersionMarker { .. }) = self;
832 pair.core_ctx()
833 }
834
835 fn datagram(&mut self) -> &mut DatagramApi<I, C, Icmp<C::BindingsContext>> {
836 let Self(pair, IpVersionMarker { .. }) = self;
837 DatagramApi::wrap(pair)
838 }
839
840 pub fn create(&mut self) -> IcmpApiSocketId<I, C>
842 where
843 <C::BindingsContext as IcmpEchoBindingsTypes>::ExternalData<I>: Default,
844 <C::BindingsContext as IcmpEchoBindingsTypes>::SocketWritableListener: Default,
845 {
846 self.create_with(Default::default(), Default::default())
847 }
848
849 pub fn create_with(
851 &mut self,
852 external_data: <C::BindingsContext as IcmpEchoBindingsTypes>::ExternalData<I>,
853 writable_listener: <C::BindingsContext as IcmpEchoBindingsTypes>::SocketWritableListener,
854 ) -> IcmpApiSocketId<I, C> {
855 self.datagram().create(external_data, writable_listener)
856 }
857
858 pub fn connect(
862 &mut self,
863 id: &IcmpApiSocketId<I, C>,
864 remote_ip: Option<
865 ZonedAddr<
866 SpecifiedAddr<I::Addr>,
867 <C::CoreContext as DeviceIdContext<AnyDevice>>::DeviceId,
868 >,
869 >,
870 remote_id: u16,
871 ) -> Result<(), datagram::ConnectError> {
872 self.datagram().connect(id, remote_ip, (), remote_id)
873 }
874
875 pub fn bind(
880 &mut self,
881 id: &IcmpApiSocketId<I, C>,
882 local_ip: Option<
883 ZonedAddr<
884 SpecifiedAddr<I::Addr>,
885 <C::CoreContext as DeviceIdContext<AnyDevice>>::DeviceId,
886 >,
887 >,
888 icmp_id: Option<NonZeroU16>,
889 ) -> Result<(), Either<ExpectedUnboundError, LocalAddressError>> {
890 self.datagram().listen(id, local_ip, icmp_id)
891 }
892
893 pub fn get_info(
895 &mut self,
896 id: &IcmpApiSocketId<I, C>,
897 ) -> datagram::SocketInfo<I::Addr, <C::CoreContext as DeviceIdContext<AnyDevice>>::WeakDeviceId>
898 {
899 self.datagram().get_info(id)
900 }
901
902 pub fn set_device(
908 &mut self,
909 id: &IcmpApiSocketId<I, C>,
910 device_id: Option<&<C::CoreContext as DeviceIdContext<AnyDevice>>::DeviceId>,
911 ) -> Result<(), SocketError> {
912 self.datagram().set_device(id, device_id)
913 }
914
915 pub fn get_bound_device(
917 &mut self,
918 id: &IcmpApiSocketId<I, C>,
919 ) -> Option<<C::CoreContext as DeviceIdContext<AnyDevice>>::WeakDeviceId> {
920 self.datagram().get_bound_device(id)
921 }
922
923 pub fn disconnect(
925 &mut self,
926 id: &IcmpApiSocketId<I, C>,
927 ) -> Result<(), datagram::ExpectedConnError> {
928 self.datagram().disconnect_connected(id)
929 }
930
931 pub fn shutdown(
933 &mut self,
934 id: &IcmpApiSocketId<I, C>,
935 shutdown_type: ShutdownType,
936 ) -> Result<(), datagram::ExpectedConnError> {
937 self.datagram().shutdown_connected(id, shutdown_type)
938 }
939
940 pub fn get_shutdown(&mut self, id: &IcmpApiSocketId<I, C>) -> Option<ShutdownType> {
942 self.datagram().get_shutdown_connected(id)
943 }
944
945 pub fn close(
947 &mut self,
948 id: IcmpApiSocketId<I, C>,
949 ) -> RemoveResourceResultWithContext<
950 <C::BindingsContext as IcmpEchoBindingsTypes>::ExternalData<I>,
951 C::BindingsContext,
952 > {
953 self.datagram().close(id)
954 }
955
956 pub fn get_unicast_hop_limit(&mut self, id: &IcmpApiSocketId<I, C>) -> NonZeroU8 {
958 self.datagram().get_ip_hop_limits(id).unicast
959 }
960
961 pub fn get_multicast_hop_limit(&mut self, id: &IcmpApiSocketId<I, C>) -> NonZeroU8 {
963 self.datagram().get_ip_hop_limits(id).multicast
964 }
965
966 pub fn set_unicast_hop_limit(
968 &mut self,
969 id: &IcmpApiSocketId<I, C>,
970 hop_limit: Option<NonZeroU8>,
971 ) {
972 self.datagram().update_ip_hop_limit(id, SocketHopLimits::set_unicast(hop_limit))
973 }
974
975 pub fn set_multicast_hop_limit(
977 &mut self,
978 id: &IcmpApiSocketId<I, C>,
979 hop_limit: Option<NonZeroU8>,
980 ) {
981 self.datagram().update_ip_hop_limit(id, SocketHopLimits::set_multicast(hop_limit))
982 }
983
984 pub fn get_multicast_loop(&mut self, id: &IcmpApiSocketId<I, C>) -> bool {
986 self.datagram().get_multicast_loop(id)
987 }
988
989 pub fn set_multicast_loop(&mut self, id: &IcmpApiSocketId<I, C>, value: bool) {
991 self.datagram().set_multicast_loop(id, value);
992 }
993
994 pub fn set_mark(&mut self, id: &IcmpApiSocketId<I, C>, domain: MarkDomain, mark: Mark) {
996 self.datagram().set_mark(id, domain, mark)
997 }
998
999 pub fn get_mark(&mut self, id: &IcmpApiSocketId<I, C>, domain: MarkDomain) -> Mark {
1001 self.datagram().get_mark(id, domain)
1002 }
1003
1004 pub fn set_send_buffer(&mut self, id: &IcmpApiSocketId<I, C>, size: usize) {
1006 self.datagram().set_send_buffer(id, size)
1007 }
1008
1009 pub fn send_buffer(&mut self, id: &IcmpApiSocketId<I, C>) -> usize {
1011 self.datagram().send_buffer(id)
1012 }
1013
1014 pub fn send<B: BufferMut>(
1018 &mut self,
1019 id: &IcmpApiSocketId<I, C>,
1020 body: B,
1021 ) -> Result<(), datagram::SendError<packet_formats::error::ParseError>> {
1022 self.datagram().send_conn(id, body)
1023 }
1024
1025 pub fn send_to<B: BufferMut>(
1029 &mut self,
1030 id: &IcmpApiSocketId<I, C>,
1031 remote_ip: Option<
1032 ZonedAddr<
1033 SpecifiedAddr<I::Addr>,
1034 <C::CoreContext as DeviceIdContext<AnyDevice>>::DeviceId,
1035 >,
1036 >,
1037 body: B,
1038 ) -> Result<
1039 (),
1040 either::Either<LocalAddressError, datagram::SendToError<packet_formats::error::ParseError>>,
1041 > {
1042 self.datagram().send_to(id, remote_ip, (), body)
1043 }
1044
1045 pub fn collect_all_sockets(&mut self) -> Vec<IcmpApiSocketId<I, C>> {
1048 self.datagram().collect_all_sockets()
1049 }
1050
1051 pub fn inspect<N>(&mut self, inspector: &mut N)
1053 where
1054 N: Inspector
1055 + InspectorDeviceExt<<C::CoreContext as DeviceIdContext<AnyDevice>>::WeakDeviceId>,
1056 for<'a> N::ChildInspector<'a>:
1057 InspectorDeviceExt<<C::CoreContext as DeviceIdContext<AnyDevice>>::WeakDeviceId>,
1058 {
1059 DatagramStateContext::for_each_socket(self.core_ctx(), |_ctx, socket_id, socket_state| {
1060 inspector.record_debug_child(socket_id, |inspector| {
1061 socket_state.record_common_info(inspector);
1062 });
1063 });
1064 }
1065}
1066
1067pub enum IcmpEchoIpTransportContext {}
1073
1074impl EchoTransportContextMarker for IcmpEchoIpTransportContext {}
1075
1076impl<I: IpExt, BC: IcmpEchoBindingsContext<I, CC::DeviceId>, CC: IcmpEchoBoundStateContext<I, BC>>
1077 IpTransportContext<I, BC, CC> for IcmpEchoIpTransportContext
1078{
1079 type EarlyDemuxSocket = Never;
1080
1081 fn early_demux<B: ParseBuffer>(
1082 _core_ctx: &mut CC,
1083 _device: &CC::DeviceId,
1084 _src_ip: I::Addr,
1085 _dst_ip: I::Addr,
1086 _buffer: B,
1087 ) -> Option<Self::EarlyDemuxSocket> {
1088 None
1089 }
1090
1091 fn receive_icmp_error(
1092 core_ctx: &mut CC,
1093 _bindings_ctx: &mut BC,
1094 _device: &CC::DeviceId,
1095 original_src_ip: Option<SpecifiedAddr<I::Addr>>,
1096 original_dst_ip: SpecifiedAddr<I::Addr>,
1097 mut original_body: &[u8],
1098 err: I::ErrorCode,
1099 ) {
1100 let echo_request = original_body
1101 .parse::<IcmpPacketRaw<I, _, IcmpEchoRequest>>()
1102 .expect("received non-echo request");
1103
1104 let original_src_ip = match original_src_ip {
1105 Some(ip) => ip,
1106 None => {
1107 trace!("IcmpIpTransportContext::receive_icmp_error: unspecified source IP address");
1108 return;
1109 }
1110 };
1111 let original_src_ip: SocketIpAddr<_> = match original_src_ip.try_into() {
1112 Ok(ip) => ip,
1113 Err(AddrIsMappedError {}) => {
1114 trace!("IcmpIpTransportContext::receive_icmp_error: mapped source IP address");
1115 return;
1116 }
1117 };
1118 let original_dst_ip: SocketIpAddr<_> = match original_dst_ip.try_into() {
1119 Ok(ip) => ip,
1120 Err(AddrIsMappedError {}) => {
1121 trace!("IcmpIpTransportContext::receive_icmp_error: mapped destination IP address");
1122 return;
1123 }
1124 };
1125
1126 let Some(id) = NonZeroU16::new(echo_request.message().id()) else {
1129 debug!(
1130 "ICMP received ICMP error {:?} from {:?}, to {:?} with an ID of 0",
1131 err, original_dst_ip, original_src_ip,
1132 );
1133 return;
1134 };
1135
1136 core_ctx.with_icmp_ctx_and_sockets_mut(|core_ctx, sockets| {
1137 if let Some(conn) = sockets.socket_map.conns().get_by_addr(&ConnAddr {
1138 ip: ConnIpAddr { local: (original_src_ip, id), remote: (original_dst_ip, ()) },
1139 device: None,
1140 }) {
1141 debug!(
1144 "ICMP received ICMP error {:?} from {:?}, to {:?} on socket {:?}",
1145 err, original_dst_ip, original_src_ip, conn
1146 );
1147 CounterContext::<IcmpRxCounters<I>>::counters(core_ctx)
1148 .error_delivered_to_socket
1149 .increment()
1150 } else {
1151 trace!(
1152 "IcmpIpTransportContext::receive_icmp_error: Got ICMP error message for \
1153 nonexistent ICMP echo socket; either the socket responsible has since been \
1154 removed, or the error message was sent in error or corrupted"
1155 );
1156 }
1157 })
1158 }
1159
1160 fn receive_ip_packet<B: BufferMut, H: IpHeaderInfo<I>>(
1161 core_ctx: &mut CC,
1162 bindings_ctx: &mut BC,
1163 device: &CC::DeviceId,
1164 src_ip: I::RecvSrcAddr,
1165 dst_ip: SpecifiedAddr<I::Addr>,
1166 mut buffer: B,
1167 info: &mut LocalDeliveryPacketInfo<I, H>,
1168 _early_demux_socket: Option<Never>,
1169 ) -> Result<(), (B, I::IcmpError)> {
1170 let LocalDeliveryPacketInfo { meta, header_info: _, marks: _ } = info;
1171 let ReceiveIpPacketMeta { broadcast: _, transparent_override, parsing_context: _ } = meta;
1172 if let Some(delivery) = transparent_override.as_ref() {
1173 unreachable!(
1174 "cannot perform transparent local delivery {delivery:?} to an ICMP socket; \
1175 transparent proxy rules can only be configured for TCP and UDP packets"
1176 );
1177 }
1178 let echo_reply =
1182 buffer.parse::<IcmpPacketRaw<I, _, IcmpEchoReply>>().expect("received non-echo reply");
1183 let Some(id) = NonZeroU16::new(echo_reply.message().id()) else { return Ok(()) };
1185
1186 let meta = echo_reply.parse_metadata();
1188 buffer.undo_parse(meta);
1189
1190 let src_ip = match SpecifiedAddr::new(src_ip.into_addr()) {
1191 Some(src_ip) => src_ip,
1192 None => {
1193 trace!("receive_icmp_echo_reply: unspecified source address");
1194 return Ok(());
1195 }
1196 };
1197 let src_ip: SocketIpAddr<_> = match src_ip.try_into() {
1198 Ok(src_ip) => src_ip,
1199 Err(AddrIsMappedError {}) => {
1200 trace!("receive_icmp_echo_reply: mapped source address");
1201 return Ok(());
1202 }
1203 };
1204 let dst_ip: SocketIpAddr<_> = match dst_ip.try_into() {
1205 Ok(dst_ip) => dst_ip,
1206 Err(AddrIsMappedError {}) => {
1207 trace!("receive_icmp_echo_reply: mapped destination address");
1208 return Ok(());
1209 }
1210 };
1211
1212 core_ctx.with_icmp_ctx_and_sockets_mut(|core_ctx, sockets| {
1213 let mut addrs_to_search = AddrVecIter::<I, CC::WeakDeviceId, IcmpAddrSpec>::with_device(
1214 ConnIpAddr { local: (dst_ip, id), remote: (src_ip, ()) }.into(),
1215 device.downgrade(),
1216 );
1217 let socket = match addrs_to_search.try_for_each(|addr_vec| {
1218 match addr_vec {
1219 AddrVec::Conn(c) => {
1220 if let Some(id) = sockets.socket_map.conns().get_by_addr(&c) {
1221 return ControlFlow::Break(id);
1222 }
1223 }
1224 AddrVec::Listen(l) => {
1225 if let Some(id) = sockets.socket_map.listeners().get_by_addr(&l) {
1226 return ControlFlow::Break(id);
1227 }
1228 }
1229 }
1230 ControlFlow::Continue(())
1231 }) {
1232 ControlFlow::Continue(()) => None,
1233 ControlFlow::Break(id) => Some(id),
1234 };
1235 if let Some(socket) = socket {
1236 trace!("receive_icmp_echo_reply: Received echo reply for local socket");
1237 match bindings_ctx.receive_icmp_echo_reply(
1238 socket,
1239 device,
1240 src_ip.addr(),
1241 dst_ip.addr(),
1242 id.get(),
1243 buffer,
1244 ) {
1245 Ok(()) => {}
1246 Err(ReceiveIcmpEchoError::QueueFull) => {
1247 core_ctx.counters().queue_full.increment();
1248 }
1249 }
1250 return;
1251 }
1252 trace!("receive_icmp_echo_reply: Received echo reply with no local socket");
1269 });
1270 Ok(())
1271 }
1272}
1273
1274#[cfg(test)]
1275mod tests {
1276 use alloc::rc::Rc;
1277 use alloc::vec;
1278 use core::cell::RefCell;
1279 use core::ops::{Deref, DerefMut};
1280
1281 use assert_matches::assert_matches;
1282 use ip_test_macro::ip_test;
1283 use net_declare::net_ip_v6;
1284 use net_types::Witness;
1285 use net_types::ip::Ipv6;
1286 use netstack3_base::socket::StrictlyZonedAddr;
1287 use netstack3_base::testutil::{
1288 FakeBindingsCtx, FakeCoreCtx, FakeDeviceId, FakeSocketWritableListener, FakeWeakDeviceId,
1289 TestIpExt,
1290 };
1291 use netstack3_base::{CtxPair, Icmpv4ErrorCode, Icmpv6ErrorCode, NetworkSerializationContext};
1292 use netstack3_ip::socket::testutil::{FakeDeviceConfig, FakeIpSocketCtx, InnerFakeIpSocketCtx};
1293 use netstack3_ip::{LocalDeliveryPacketInfo, SendIpPacketMeta};
1294 use packet::{Buf, EmptyBuf, NestableSerializer as _, Serializer};
1295 use packet_formats::icmp::{
1296 IcmpDestUnreachable, IcmpPacket, IcmpParseArgs, IcmpZeroCode, Icmpv4DestUnreachableCode,
1297 Icmpv6DestUnreachableCode,
1298 };
1299
1300 use super::*;
1301
1302 const REMOTE_ID: u16 = 27;
1303 const ICMP_ID: NonZeroU16 = NonZeroU16::new(10).unwrap();
1304 const SEQ_NUM: u16 = 0xF0;
1305
1306 impl<I: IpExt, D: WeakDeviceIdentifier, BT: IcmpEchoBindingsTypes> IcmpSocketId<I, D, BT> {
1308 fn get(&self) -> impl Deref<Target = IcmpSocketState<I, D, BT>> + '_ {
1309 self.state().read()
1310 }
1311
1312 fn get_mut(&self) -> impl DerefMut<Target = IcmpSocketState<I, D, BT>> + '_ {
1313 self.state().write()
1314 }
1315 }
1316
1317 struct FakeIcmpCoreCtxState<I: IpExt> {
1318 bound_sockets:
1319 Rc<RefCell<BoundSockets<I, FakeWeakDeviceId<FakeDeviceId>, FakeIcmpBindingsCtx<I>>>>,
1320 all_sockets: IcmpSocketSet<I, FakeWeakDeviceId<FakeDeviceId>, FakeIcmpBindingsCtx<I>>,
1321 ip_socket_ctx: FakeIpSocketCtx<I, FakeDeviceId>,
1322 rx_counters: IcmpRxCounters<I>,
1323 }
1324
1325 impl<I: IpExt> InnerFakeIpSocketCtx<I, FakeDeviceId> for FakeIcmpCoreCtxState<I> {
1326 fn fake_ip_socket_ctx_mut(&mut self) -> &mut FakeIpSocketCtx<I, FakeDeviceId> {
1327 &mut self.ip_socket_ctx
1328 }
1329 }
1330
1331 impl<I: IpExt + TestIpExt> Default for FakeIcmpCoreCtxState<I> {
1332 fn default() -> Self {
1333 Self {
1334 bound_sockets: Default::default(),
1335 all_sockets: Default::default(),
1336 ip_socket_ctx: FakeIpSocketCtx::new(core::iter::once(FakeDeviceConfig {
1337 device: FakeDeviceId,
1338 local_ips: vec![I::TEST_ADDRS.local_ip],
1339 remote_ips: vec![I::TEST_ADDRS.remote_ip],
1340 })),
1341 rx_counters: Default::default(),
1342 }
1343 }
1344 }
1345
1346 type FakeIcmpCoreCtx<I> = FakeCoreCtx<
1347 FakeIcmpCoreCtxState<I>,
1348 SendIpPacketMeta<I, FakeDeviceId, SpecifiedAddr<<I as Ip>::Addr>>,
1349 FakeDeviceId,
1350 >;
1351 type FakeIcmpBindingsCtx<I> = FakeBindingsCtx<(), (), FakeIcmpBindingsCtxState<I>, ()>;
1352 type FakeIcmpCtx<I> = CtxPair<FakeIcmpCoreCtx<I>, FakeIcmpBindingsCtx<I>>;
1353
1354 #[derive(Derivative)]
1355 #[derivative(Default)]
1356
1357 struct FakeIcmpBindingsCtxState<I: IpExt> {
1358 received: Vec<ReceivedEchoPacket<I>>,
1359 #[derivative(Default(value = "usize::MAX"))]
1360 max_size: usize,
1361 }
1362
1363 #[derive(Debug)]
1364 struct ReceivedEchoPacket<I: IpExt> {
1365 src_ip: I::Addr,
1366 dst_ip: I::Addr,
1367 socket: IcmpSocketId<I, FakeWeakDeviceId<FakeDeviceId>, FakeIcmpBindingsCtx<I>>,
1368 id: u16,
1369 data: Vec<u8>,
1370 }
1371
1372 impl<I: IpExt> IcmpEchoContextMarker for FakeIcmpCoreCtx<I> {}
1373
1374 impl<I: IpExt> CounterContext<IcmpRxCounters<I>> for FakeIcmpCoreCtxState<I> {
1375 fn counters(&self) -> &IcmpRxCounters<I> {
1376 &self.rx_counters
1377 }
1378 }
1379
1380 impl<I: IpExt> IcmpEchoBoundStateContext<I, FakeIcmpBindingsCtx<I>> for FakeIcmpCoreCtx<I> {
1381 type IpSocketsCtx<'a> = Self;
1382
1383 fn with_icmp_ctx_and_sockets_mut<
1384 O,
1385 F: FnOnce(
1386 &mut Self::IpSocketsCtx<'_>,
1387 &mut BoundSockets<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>,
1388 ) -> O,
1389 >(
1390 &mut self,
1391 cb: F,
1392 ) -> O {
1393 let bound_sockets = self.state.bound_sockets.clone();
1394 let mut bound_sockets = bound_sockets.borrow_mut();
1395 cb(self, &mut bound_sockets)
1396 }
1397 }
1398
1399 impl<I: IpExt> IcmpEchoStateContext<I, FakeIcmpBindingsCtx<I>> for FakeIcmpCoreCtx<I> {
1400 type SocketStateCtx<'a> = Self;
1401
1402 fn with_all_sockets_mut<
1403 O,
1404 F: FnOnce(&mut IcmpSocketSet<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>) -> O,
1405 >(
1406 &mut self,
1407 cb: F,
1408 ) -> O {
1409 cb(&mut self.state.all_sockets)
1410 }
1411
1412 fn with_all_sockets<
1413 O,
1414 F: FnOnce(&IcmpSocketSet<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>) -> O,
1415 >(
1416 &mut self,
1417 cb: F,
1418 ) -> O {
1419 cb(&self.state.all_sockets)
1420 }
1421
1422 fn with_socket_state<
1423 O,
1424 F: FnOnce(
1425 &mut Self::SocketStateCtx<'_>,
1426 &IcmpSocketState<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>,
1427 ) -> O,
1428 >(
1429 &mut self,
1430 id: &IcmpSocketId<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>,
1431 cb: F,
1432 ) -> O {
1433 cb(self, &id.get())
1434 }
1435
1436 fn with_socket_state_mut<
1437 O,
1438 F: FnOnce(
1439 &mut Self::SocketStateCtx<'_>,
1440 &mut IcmpSocketState<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>,
1441 ) -> O,
1442 >(
1443 &mut self,
1444 id: &IcmpSocketId<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>,
1445 cb: F,
1446 ) -> O {
1447 cb(self, &mut id.get_mut())
1448 }
1449
1450 fn with_bound_state_context<O, F: FnOnce(&mut Self::SocketStateCtx<'_>) -> O>(
1451 &mut self,
1452 cb: F,
1453 ) -> O {
1454 cb(self)
1455 }
1456
1457 fn for_each_socket<
1458 F: FnMut(
1459 &mut Self::SocketStateCtx<'_>,
1460 &IcmpSocketId<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>,
1461 &IcmpSocketState<I, Self::WeakDeviceId, FakeIcmpBindingsCtx<I>>,
1462 ),
1463 >(
1464 &mut self,
1465 mut cb: F,
1466 ) {
1467 let socks = self
1468 .state
1469 .all_sockets
1470 .keys()
1471 .map(|id| IcmpSocketId::from(id.clone()))
1472 .collect::<Vec<_>>();
1473 for id in socks {
1474 cb(self, &id, &id.get());
1475 }
1476 }
1477 }
1478
1479 impl<I: IpExt> IcmpEchoBindingsContext<I, FakeDeviceId> for FakeIcmpBindingsCtx<I> {
1480 fn receive_icmp_echo_reply<B: BufferMut>(
1481 &mut self,
1482 socket: &IcmpSocketId<I, FakeWeakDeviceId<FakeDeviceId>, FakeIcmpBindingsCtx<I>>,
1483 _device_id: &FakeDeviceId,
1484 src_ip: I::Addr,
1485 dst_ip: I::Addr,
1486 id: u16,
1487 data: B,
1488 ) -> Result<(), ReceiveIcmpEchoError> {
1489 if self.state.received.len() < self.state.max_size {
1490 self.state.received.push(ReceivedEchoPacket {
1491 src_ip,
1492 dst_ip,
1493 id,
1494 data: data.to_flattened_vec(),
1495 socket: socket.clone(),
1496 });
1497 Ok(())
1498 } else {
1499 Err(ReceiveIcmpEchoError::QueueFull)
1500 }
1501 }
1502 }
1503
1504 impl<I: IpExt> IcmpEchoBindingsTypes for FakeIcmpBindingsCtx<I> {
1505 type ExternalData<II: Ip> = ();
1506 type SocketWritableListener = FakeSocketWritableListener;
1507 }
1508
1509 #[test]
1510 fn test_connect_dual_stack_fails() {
1511 let mut ctx = FakeIcmpCtx::<Ipv6>::default();
1514 let mut api = IcmpEchoSocketApi::<Ipv6, _>::new(ctx.as_mut());
1515 let conn = api.create();
1516 assert_eq!(
1517 api.connect(
1518 &conn,
1519 Some(ZonedAddr::Unzoned(
1520 SpecifiedAddr::new(net_ip_v6!("::ffff:192.0.2.1")).unwrap(),
1521 )),
1522 REMOTE_ID,
1523 ),
1524 Err(datagram::ConnectError::RemoteUnexpectedlyMapped)
1525 );
1526 }
1527
1528 #[ip_test(I)]
1529 fn send_invalid_icmp_echo<I: TestIpExt + IpExt>() {
1530 let mut ctx = FakeIcmpCtx::<I>::default();
1531 let mut api = IcmpEchoSocketApi::<I, _>::new(ctx.as_mut());
1532 let conn = api.create();
1533 api.connect(&conn, Some(ZonedAddr::Unzoned(I::TEST_ADDRS.remote_ip)), REMOTE_ID).unwrap();
1534
1535 let pb = IcmpPacketBuilder::<I, _>::new(
1536 I::TEST_ADDRS.local_ip.get(),
1537 I::TEST_ADDRS.remote_ip.get(),
1538 IcmpZeroCode,
1539 packet_formats::icmp::IcmpEchoReply::new(0, 1),
1540 );
1541 let buf = pb
1542 .wrap_body(Buf::new(Vec::new(), ..))
1543 .serialize_vec_outer(&mut NetworkSerializationContext::default())
1544 .unwrap()
1545 .into_inner();
1546 assert_matches!(
1547 api.send(&conn, buf),
1548 Err(datagram::SendError::SerializeError(
1549 packet_formats::error::ParseError::NotExpected
1550 ))
1551 );
1552 }
1553
1554 #[ip_test(I)]
1555 fn get_info<I: TestIpExt + IpExt>() {
1556 let mut ctx = FakeIcmpCtx::<I>::default();
1557 let mut api = IcmpEchoSocketApi::<I, _>::new(ctx.as_mut());
1558
1559 let id = api.create();
1560 assert_eq!(api.get_info(&id), datagram::SocketInfo::Unbound);
1561
1562 api.bind(&id, None, Some(ICMP_ID)).unwrap();
1563 assert_eq!(
1564 api.get_info(&id),
1565 datagram::SocketInfo::Listener(datagram::ListenerInfo {
1566 local_ip: None,
1567 local_identifier: ICMP_ID
1568 })
1569 );
1570
1571 api.connect(&id, Some(ZonedAddr::Unzoned(I::TEST_ADDRS.remote_ip)), REMOTE_ID).unwrap();
1572 assert_eq!(
1573 api.get_info(&id),
1574 datagram::SocketInfo::Connected(datagram::ConnInfo {
1575 local_ip: StrictlyZonedAddr::new_unzoned_or_panic(I::TEST_ADDRS.local_ip),
1576 local_identifier: ICMP_ID,
1577 remote_ip: StrictlyZonedAddr::new_unzoned_or_panic(I::TEST_ADDRS.remote_ip),
1578 remote_identifier: REMOTE_ID,
1579 })
1580 );
1581 }
1582
1583 #[ip_test(I)]
1584 fn send<I: TestIpExt + IpExt>() {
1585 let mut ctx = FakeIcmpCtx::<I>::default();
1586 let mut api = IcmpEchoSocketApi::<I, _>::new(ctx.as_mut());
1587 let sock = api.create();
1588
1589 api.bind(&sock, Some(ZonedAddr::Unzoned(I::TEST_ADDRS.local_ip)), Some(ICMP_ID)).unwrap();
1590 api.connect(&sock, Some(ZonedAddr::Unzoned(I::TEST_ADDRS.remote_ip)), REMOTE_ID).unwrap();
1591
1592 let packet = Buf::new([1u8, 2, 3, 4], ..)
1593 .wrap_in(IcmpPacketBuilder::<I, _>::new(
1594 I::UNSPECIFIED_ADDRESS,
1595 I::UNSPECIFIED_ADDRESS,
1596 IcmpZeroCode,
1597 IcmpEchoRequest::new(0, SEQ_NUM),
1599 ))
1600 .serialize_vec_outer(&mut NetworkSerializationContext::default())
1601 .unwrap()
1602 .unwrap_b();
1603 api.send(&sock, Buf::new(packet, ..)).unwrap();
1604 let frames = ctx.core_ctx.frames.take_frames();
1605 let (SendIpPacketMeta { device: _, src_ip, dst_ip, .. }, body) =
1606 assert_matches!(&frames[..], [f] => f);
1607 assert_eq!(dst_ip, &I::TEST_ADDRS.remote_ip);
1608
1609 let mut body = &body[..];
1610 let echo_req: IcmpPacket<I, _, IcmpEchoRequest> =
1611 body.parse_with(IcmpParseArgs::new(src_ip.get(), dst_ip.get())).unwrap();
1612 assert_eq!(echo_req.message().id(), ICMP_ID.get());
1613 assert_eq!(echo_req.message().seq(), SEQ_NUM);
1614 }
1615
1616 #[ip_test(I)]
1617 fn receive<I: TestIpExt + IpExt>() {
1618 let mut ctx = FakeIcmpCtx::<I>::default();
1619 let mut api = IcmpEchoSocketApi::<I, _>::new(ctx.as_mut());
1620 let sock = api.create();
1621
1622 api.bind(&sock, Some(ZonedAddr::Unzoned(I::TEST_ADDRS.local_ip)), Some(ICMP_ID)).unwrap();
1623
1624 let reply = IcmpPacketBuilder::<I, _>::new(
1625 I::UNSPECIFIED_ADDRESS,
1627 I::UNSPECIFIED_ADDRESS,
1628 IcmpZeroCode,
1629 IcmpEchoReply::new(ICMP_ID.get(), SEQ_NUM),
1630 )
1631 .wrap_body(Buf::new([1u8, 2, 3, 4], ..))
1632 .serialize_vec_outer(&mut NetworkSerializationContext::default())
1633 .unwrap();
1634
1635 let CtxPair { core_ctx, bindings_ctx } = &mut ctx;
1636 let src_ip = I::TEST_ADDRS.remote_ip;
1637 let dst_ip = I::TEST_ADDRS.local_ip;
1638 <IcmpEchoIpTransportContext as IpTransportContext<I, _, _>>::receive_ip_packet(
1639 core_ctx,
1640 bindings_ctx,
1641 &FakeDeviceId,
1642 I::RecvSrcAddr::new(src_ip.get()).unwrap(),
1643 dst_ip,
1644 reply.clone(),
1645 &mut LocalDeliveryPacketInfo::default(),
1646 None,
1647 )
1648 .unwrap();
1649
1650 let received = core::mem::take(&mut bindings_ctx.state.received);
1651 let ReceivedEchoPacket {
1652 src_ip: got_src_ip,
1653 dst_ip: got_dst_ip,
1654 socket: got_socket,
1655 id: got_id,
1656 data: got_data,
1657 } = assert_matches!(&received[..], [f] => f);
1658 assert_eq!(got_src_ip, &src_ip.get());
1659 assert_eq!(got_dst_ip, &dst_ip.get());
1660 assert_eq!(got_socket, &sock);
1661 assert_eq!(got_id, &ICMP_ID.get());
1662 assert_eq!(&got_data[..], reply.as_ref());
1663 }
1664
1665 #[ip_test(I)]
1666 fn receive_no_socket<I: TestIpExt + IpExt>() {
1667 let mut ctx = FakeIcmpCtx::<I>::default();
1668 let mut api = IcmpEchoSocketApi::<I, _>::new(ctx.as_mut());
1669 let sock = api.create();
1670
1671 const BIND_ICMP_ID: NonZeroU16 = NonZeroU16::new(10).unwrap();
1672 const OTHER_ICMP_ID: NonZeroU16 = NonZeroU16::new(16).unwrap();
1673
1674 api.bind(&sock, Some(ZonedAddr::Unzoned(I::TEST_ADDRS.local_ip)), Some(BIND_ICMP_ID))
1675 .unwrap();
1676
1677 let reply = IcmpPacketBuilder::<I, _>::new(
1678 I::UNSPECIFIED_ADDRESS,
1680 I::UNSPECIFIED_ADDRESS,
1681 IcmpZeroCode,
1682 IcmpEchoReply::new(OTHER_ICMP_ID.get(), SEQ_NUM),
1683 )
1684 .wrap_body(EmptyBuf)
1685 .serialize_vec_outer(&mut NetworkSerializationContext::default())
1686 .unwrap();
1687
1688 let CtxPair { core_ctx, bindings_ctx } = &mut ctx;
1689 <IcmpEchoIpTransportContext as IpTransportContext<I, _, _>>::receive_ip_packet(
1690 core_ctx,
1691 bindings_ctx,
1692 &FakeDeviceId,
1693 I::RecvSrcAddr::new(I::TEST_ADDRS.remote_ip.get()).unwrap(),
1694 I::TEST_ADDRS.local_ip,
1695 reply,
1696 &mut LocalDeliveryPacketInfo::default(),
1697 None,
1698 )
1699 .unwrap();
1700 assert_matches!(&bindings_ctx.state.received[..], []);
1701 }
1702
1703 #[ip_test(I)]
1704 fn receive_queue_full<I: TestIpExt + IpExt>() {
1705 let mut ctx = FakeIcmpCtx::<I>::default();
1706 let mut api = IcmpEchoSocketApi::<I, _>::new(ctx.as_mut());
1707 let sock = api.create();
1708
1709 api.bind(&sock, Some(ZonedAddr::Unzoned(I::TEST_ADDRS.local_ip)), Some(ICMP_ID)).unwrap();
1710
1711 let CtxPair { core_ctx, bindings_ctx } = &mut ctx;
1713 bindings_ctx.state.max_size = 0;
1714
1715 let reply = IcmpPacketBuilder::<I, _>::new(
1716 I::UNSPECIFIED_ADDRESS,
1718 I::UNSPECIFIED_ADDRESS,
1719 IcmpZeroCode,
1720 IcmpEchoReply::new(ICMP_ID.get(), SEQ_NUM),
1721 )
1722 .wrap_body(Buf::new([1u8, 2, 3, 4], ..))
1723 .serialize_vec_outer(&mut NetworkSerializationContext::default())
1724 .unwrap();
1725
1726 let src_ip = I::TEST_ADDRS.remote_ip;
1727 let dst_ip = I::TEST_ADDRS.local_ip;
1728 <IcmpEchoIpTransportContext as IpTransportContext<I, _, _>>::receive_ip_packet(
1729 core_ctx,
1730 bindings_ctx,
1731 &FakeDeviceId,
1732 I::RecvSrcAddr::new(src_ip.get()).unwrap(),
1733 dst_ip,
1734 reply,
1735 &mut LocalDeliveryPacketInfo::default(),
1736 None,
1737 )
1738 .unwrap();
1739
1740 assert_eq!(core_ctx.counters().queue_full.get(), 1);
1741 }
1742
1743 #[ip_test(I, test = false)]
1744 #[test_case::test_matrix(
1745 [MarkDomain::Mark1, MarkDomain::Mark2],
1746 [None, Some(0), Some(1)]
1747 )]
1748 fn icmp_socket_marks<I: TestIpExt + IpExt>(domain: MarkDomain, mark: Option<u32>) {
1749 let mut ctx = FakeIcmpCtx::<I>::default();
1750 let mut api = IcmpEchoSocketApi::<I, _>::new(ctx.as_mut());
1751 let socket = api.create();
1752
1753 assert_eq!(api.get_mark(&socket, domain), Mark(None));
1755
1756 let mark = Mark(mark);
1757 api.set_mark(&socket, domain, mark);
1759 assert_eq!(api.get_mark(&socket, domain), mark);
1760 }
1761
1762 #[ip_test(I)]
1768 fn icmp_error_with_inner_icmp_echo_with_id_0<I: TestIpExt + IpExt>() {
1769 let mut ctx = FakeIcmpCtx::<I>::default();
1770
1771 let src_ip = I::TEST_ADDRS.remote_ip;
1772 let dst_ip = I::TEST_ADDRS.local_ip;
1773
1774 let original_body = Buf::new([1, 2, 3, 4], ..)
1775 .wrap_in(IcmpPacketBuilder::<I, _>::new(
1776 *src_ip,
1777 *dst_ip,
1778 IcmpZeroCode,
1779 IcmpEchoRequest::new(0, 1),
1782 ))
1783 .serialize_vec_outer(&mut NetworkSerializationContext::default())
1784 .unwrap()
1785 .unwrap_b();
1786
1787 let CtxPair { core_ctx, bindings_ctx } = &mut ctx;
1788 <IcmpEchoIpTransportContext as IpTransportContext<I, _, _>>::receive_icmp_error(
1789 core_ctx,
1790 bindings_ctx,
1791 &FakeDeviceId,
1792 Some(src_ip),
1793 dst_ip,
1794 original_body.as_ref(),
1795 I::map_ip_out(
1796 (),
1797 |()| {
1798 Icmpv4ErrorCode::DestUnreachable(
1799 Icmpv4DestUnreachableCode::DestNetworkUnreachable,
1800 IcmpDestUnreachable::default(),
1801 )
1802 },
1803 |()| Icmpv6ErrorCode::DestUnreachable(Icmpv6DestUnreachableCode::AddrUnreachable),
1804 ),
1805 )
1806 }
1807}