pocketscion 0.5.2

A lightweight SCION network simulator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2025 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Local Network Simulation
//!
//! Simulates a specific routers dispatching or SCMP request behaviour

use anyhow::{Context, bail};
use bytes::Bytes;
use scion_proto::{
    address::{IsdAsn, ScionAddr, SocketAddr},
    packet::{
        ByEndpoint, ScionPacketRaw, ScionPacketScmp, ScionPacketUdp, classify_scion_packet,
        layout::ScionPacketOffset,
    },
    path::{DataPlanePath, PathType, StandardPath},
    scmp::{
        DestinationUnreachableCode, ParameterProblemCode, ScmpDestinationUnreachable,
        ScmpEchoReply, ScmpErrorMessage, ScmpMessage, ScmpMessageBase, ScmpParameterProblem,
        ScmpTracerouteReply,
    },
    wire_encoding::WireEncodeVec,
};
use scion_protobuf::control_plane::v1::{ServiceResolutionResponse, Transport};
use sciparse::{
    core::view::View,
    path::onehop::{model::OneHopPath, view::OneHopPathView},
};
use tracing::info_span;

use crate::network::{
    local::{external_as_registry::ExternalAsRegistry, receiver_registry::NetworkReceiverRegistry},
    scion::{
        routing::LocalAsRoutingAction,
        topology::{ScionGlobalInterfaceId, ScionRouter},
    },
};

/// A local network simulation.
pub struct LocalNetworkSimulation<'input> {
    /// The router for which this simulation is running
    router: &'input ScionRouter,
    /// The AS for which this simulation is running
    local_as: IsdAsn,
    /// The interface for which this simulation is running
    local_if_id: u16,
    /// Dispatchers available to the simulation.
    receivers: &'input NetworkReceiverRegistry,
    /// Registry of external ASes, needed for forwarding to external ASes
    external_ases: &'input ExternalAsRegistry,
}

impl LocalNetworkSimulation<'_> {
    /// Creates a new Simulator at given AS and Interface
    pub fn new<'input>(
        local_as: IsdAsn,
        local_if_id: u16,
        receivers: &'input NetworkReceiverRegistry,
        external_ases: &'input ExternalAsRegistry,
        router: &'input ScionRouter,
    ) -> LocalNetworkSimulation<'input> {
        LocalNetworkSimulation {
            local_if_id,
            local_as,
            receivers,
            external_ases,
            router,
        }
    }
}

/// Effect of a dispatched packet
pub enum DispatchEffect {
    /// A SCMP reply should be sent back
    ScmpReply(ScmpErrorMessage),
    /// Some other reply should be sent back
    OtherReply {
        /// The reply payload
        payload: Vec<u8>,
    },
}

impl LocalNetworkSimulation<'_> {
    /// Best effort dispatch of a packet into given local AS.
    ///
    /// Reads destination from packet.
    pub fn dispatch(&self, packet: ScionPacketRaw) -> Option<DispatchEffect> {
        tracing::trace!(local_as = %self.local_as, "Dispatching packet into AS");
        // Get Dest Addr
        let Some(dest_addr) = packet.headers.address.destination() else {
            tracing::warn!("No local address found in packet destination, cannot dispatch");
            return Some(DispatchEffect::ScmpReply(
                ScmpDestinationUnreachable::new(
                    DestinationUnreachableCode::AddressUnreachable,
                    packet.encode_to_bytes_vec().concat().into(),
                )
                .into(),
            ));
        };

        // Can't handle if non local
        if dest_addr.isd_asn() != self.local_as {
            tracing::warn!(
                dest_as = %dest_addr.isd_asn(),
                local_as = %self.local_as,
                "Packet destination AS does not match local AS, cannot dispatch"
            );

            return Some(DispatchEffect::ScmpReply(
                ScmpParameterProblem::new(
                    ParameterProblemCode::NonLocalDelivery,
                    ScionPacketOffset::address_header().dst_host_addr().bytes(),
                    packet.encode_to_bytes_vec().concat().into(),
                )
                .into(),
            ));
        }

        // Maybe do service resolution
        if let ScionAddr::Svc(dst_svc) = dest_addr {
            // XXX: This is usually not done in the router, but the control service, for simplicity
            // we do it here.
            use prost::Message;

            if let Some(transports) = self
                .receivers
                .svc_mappings(dest_addr.isd_asn(), dst_svc.host())
            {
                let reply = ServiceResolutionResponse {
                    transports: transports
                        .iter()
                        .map(|(protocol, socket_addr)| {
                            (
                                protocol.clone(),
                                Transport {
                                    address: socket_addr.to_string(),
                                },
                            )
                        })
                        .collect(),
                };

                let reply = reply.encode_to_vec();
                return Some(DispatchEffect::OtherReply { payload: reply });
            } else {
                tracing::debug!(
                    "received packet with SVC destination {}, but no mapping found",
                    dst_svc
                );
                return Some(DispatchEffect::OtherReply {
                    payload: ServiceResolutionResponse::default().encode_to_vec(),
                });
            }
        }

        let local_addr = dest_addr
            .local_address()
            .expect("checked above that dest is not SVC");

        // Try dispatch
        let Some(receiver) = self.receivers.by_addr(self.local_as, local_addr) else {
            tracing::warn!(%dest_addr, "No dispatcher found");
            return Some(DispatchEffect::ScmpReply(
                ScmpDestinationUnreachable::new(
                    DestinationUnreachableCode::AddressUnreachable,
                    packet.encode_to_bytes_vec().concat().into(),
                )
                .into(),
            ));
        };

        receiver.receive_packet(packet);

        None
    }

    /// Handles a Routing Action at this specific router
    ///
    /// `action` the local routing action
    /// `packet` the packet for this action
    ///
    /// Returns
    /// - Error If the Simulation failed
    /// - Some  If a response should send
    pub fn handle_local_routing_action(
        &self,
        action: LocalAsRoutingAction,
        packet: ScionPacketRaw,
    ) -> anyhow::Result<Option<ScionPacketRaw>> {
        let pkt_source_as = packet
            .headers
            .address
            .source()
            .map(|s| s.isd_asn())
            .unwrap_or(IsdAsn(0));

        let reply: Option<ScionPacketRaw> = match action {
            LocalAsRoutingAction::ForwardLocal { target_address: _ } => {
                // TODO: Should inspect the target address and see if it is meant for this router.
                match self.dispatch(packet.clone()) {
                    None => None,
                    Some(DispatchEffect::ScmpReply(scmp_reply)) => {
                        maybe_create_scmp_reply(
                            self.local_as,
                            self.router,
                            scmp_reply.into(),
                            packet,
                        )
                        .context("error creating SCMP reply after dispatching")?
                        .map(Into::into)
                    }
                    // XXX: this is used for SVC resolution, this is usually not done in the router,
                    // but the control service, for simplicity we do it here.
                    Some(DispatchEffect::OtherReply { payload }) => {
                        create_udp_reply(self.local_as, self.router, payload, packet)
                            .context("error creating UDP reply after dispatching")?
                            .into()
                    }
                }
            }
            LocalAsRoutingAction::SendSCMPErrorResponse(scmp_error_message) => {
                maybe_create_scmp_reply(
                    self.local_as,
                    self.router,
                    scmp_error_message.into(),
                    packet,
                )?
                .map(Into::into)
            }
            LocalAsRoutingAction::IngressSCMPHandleRequest { interface_id } => {
                debug_assert_eq!(
                    self.local_if_id, interface_id,
                    "This should always be the interface of the router"
                );
                self.handle_scmp(false, packet)
                    .context("error handling SCMP request")?
                    .map(Into::into)
            }
            LocalAsRoutingAction::EgressSCMPHandleRequest { interface_id } => {
                debug_assert_eq!(
                    self.local_if_id, interface_id,
                    "This should always be the interface of the router"
                );
                self.handle_scmp(true, packet)
                    .context("error handling SCMP request")?
                    .map(Into::into)
            }
            LocalAsRoutingAction::ForwardExternal {
                sim_egress_interface_id,
                extern_ingress_interface_id,
                external_as,
            } => {
                match self.external_ases.get(&external_as) {
                    Some(adapter) => {
                        adapter.handle_incoming_packet(
                            ScionGlobalInterfaceId {
                                isd_as: self.local_as,
                                if_id: sim_egress_interface_id,
                            },
                            ScionGlobalInterfaceId {
                                isd_as: external_as,
                                if_id: extern_ingress_interface_id,
                            },
                            &mut packet.clone(),
                        )
                    }
                    None => {
                        tracing::info!(
                            external_as = %external_as,
                            "No adapter found for external AS, dropping packet"
                        );
                    }
                }

                return Ok(None);
            }
        };

        let Some(reply) = reply else {
            // No reply, we are done
            return Ok(None);
        };

        if pkt_source_as != self.local_as {
            // Packet needs to be dispatched through SCION Network
            return Ok(Some(reply));
        }

        // Packet comes from this AS, dispatch, we don't generate any responses for this case
        match self.dispatch(reply) {
            None => {}
            Some(DispatchEffect::ScmpReply(_)) => {
                tracing::warn!("Internal AS dispatch generated SCMP reply, not forwarding");
            }
            Some(DispatchEffect::OtherReply { .. }) => {
                tracing::warn!("Internal AS dispatch generated reply, not forwarding");
            }
        };

        Ok(None) // Handling complete
    }

    /// Handles an incoming SCMP packet, generating a reply if needed.
    pub fn handle_scmp(
        &self,
        egress: bool,
        packet: ScionPacketRaw,
    ) -> anyhow::Result<Option<ScionPacketScmp>> {
        // TODO: SCMP should inspect the dst address to determine if the packet is meant for this
        // router.
        let _s = info_span!(
            "loc-scmp",
            local = %self.local_as,
            iid = self.local_if_id,
            egress = egress
        )
        .entered();

        tracing::trace!("Handling SCMP");
        let request = classify_scion_packet(packet)
            .context("error classifying SCION packet for SCMP response")?
            .try_into_scmp()
            .map_err(|_| anyhow::anyhow!("packet was not a SCMP message"))?;

        match &request.message {
            ScmpMessage::EchoRequest(scmp_echo_request) => {
                tracing::trace!("Handling SCMP echo request");
                maybe_create_scmp_reply(
                    self.local_as,
                    self.router,
                    ScmpMessage::EchoReply(ScmpEchoReply::new(
                        scmp_echo_request.identifier,
                        scmp_echo_request.sequence_number,
                        scmp_echo_request.data.clone(),
                    )),
                    request.into(),
                )
            }
            ScmpMessage::TracerouteRequest(scmp_traceroute_request) => {
                tracing::trace!("Handling SCMP traceroute request");
                maybe_create_scmp_reply(
                    self.local_as,
                    self.router,
                    ScmpMessage::TracerouteReply(ScmpTracerouteReply::new(
                        scmp_traceroute_request.identifier,
                        scmp_traceroute_request.sequence_number,
                        self.local_as,
                        self.local_if_id as u64,
                    )),
                    request.into(),
                )
            }
            _ => {
                tracing::warn!(message = ?request.message, "Received unexpected SCMP message");

                bail!("Unexpected SCMP message");
            }
        }
    }
}

fn create_udp_reply(
    local_as: IsdAsn,
    router: &ScionRouter,
    payload: Vec<u8>,
    respond_to: ScionPacketRaw,
) -> anyhow::Result<ScionPacketRaw> {
    let respond_to_udp: ScionPacketUdp = respond_to
        .try_into()
        .context("packet is not a UDP packet, cannot create reply")?;

    // Note: if src address is a multicast address, this should not generate a response.
    let packet_src = respond_to_udp
        .source()
        .context("UDP packet has no source socket address")?;
    let endhosts = ByEndpoint::<SocketAddr> {
        source: SocketAddr::new(ScionAddr::new(local_as, router.ip.into()), 0),
        destination: packet_src,
    };

    let path = if packet_src.isd_asn() == local_as {
        // If we send packet locally, empty path is fine
        DataPlanePath::EmptyPath
    } else if let DataPlanePath::Unsupported {
        path_type: PathType::OneHop,
        bytes,
    } = respond_to_udp.headers.path
    {
        // One hop path gets updated to a standard path
        let (ohp, _) =
            OneHopPathView::from_slice(&bytes).context("error parsing one-hop path from packet")?;
        let ohp = OneHopPath::from_view(ohp);

        let rev_std_path = ohp.into_reversed_standard_path().map_err(|_| {
            anyhow::anyhow!("error converting one-hop path to standard path for reply")
        })?;
        let rev_std_path = StandardPath::from_sciparse_standard_path(rev_std_path);
        DataPlanePath::Standard(rev_std_path.into())
    } else {
        // Otherwise reverse the path
        let mut path = respond_to_udp.headers.path.clone();
        path.reverse().context("error reversing path from packet")?;

        path
    };

    Ok(
        ScionPacketUdp::new(endhosts, path, Bytes::from_owner(payload))
            .context("error creating reply packet")?
            .into(),
    )
}

/// Creates a SCMP Response to given packet
///
/// If the packet is a SCMP Error Message, no response is created.
fn maybe_create_scmp_reply(
    local_as: IsdAsn,
    router: &ScionRouter,
    scmp: ScmpMessage,
    respond_to: ScionPacketRaw,
) -> anyhow::Result<Option<ScionPacketScmp>> {
    let classify = classify_scion_packet(respond_to.clone())
        .context("error classifying SCION packet for SCMP response")?;

    match classify {
        // If the packet is a SCMP Error Message, we do not create a response
        scion_proto::packet::PacketClassification::ScmpWithDestination(_, pkt)
        | scion_proto::packet::PacketClassification::ScmpWithoutDestination(pkt)
            if pkt.message.is_error() =>
        {
            return Ok(None);
        }
        _ => {}
    }

    let packet_src = respond_to
        .headers
        .address
        .source()
        .context("packet has no source address")?;

    // Note: if src address is a multicast address, this should not generate a response.

    let endhosts = ByEndpoint::<ScionAddr> {
        source: ScionAddr::new(local_as, router.ip.into()),
        destination: packet_src,
    };

    let path = if packet_src.isd_asn() == local_as {
        // If we send packet locally, empty path is fine
        DataPlanePath::EmptyPath
    } else if let DataPlanePath::Unsupported {
        path_type: PathType::OneHop,
        bytes,
    } = respond_to.headers.path
    {
        // One hop path gets updated to a standard path
        let (ohp, _) =
            OneHopPathView::from_slice(&bytes).context("error parsing one-hop path from packet")?;
        let ohp = OneHopPath::from_view(ohp);

        let rev_std_path = ohp.into_reversed_standard_path().map_err(|_| {
            anyhow::anyhow!("error converting one-hop path to standard path for reply")
        })?;
        let rev_std_path = StandardPath::from_sciparse_standard_path(rev_std_path);
        DataPlanePath::Standard(rev_std_path.into())
    } else {
        // Otherwise reverse the path
        let mut path = respond_to.headers.path.clone();
        path.reverse().context("error reversing path from packet")?;

        path
    };

    ScionPacketScmp::new(endhosts, path, scmp)
        .context("error creating SCMP packet")
        .map(Some)
}