snap_dataplane/tunnel_gateway.rs
1// Copyright 2026 Anapaya Systems
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! Tunnel gateway
15
16use std::sync::Arc;
17
18use scion_sdk_utils::task_handler::CancelTaskSet;
19use snap_tun::server::SnapTunAuthorization;
20use tokio::net::UdpSocket;
21
22use crate::{
23 dispatcher::Dispatcher,
24 tunnel_gateway::{dispatcher::TunnelGatewayDispatcherReceiver, gateway::TunnelGateway},
25};
26
27pub mod dispatcher;
28pub mod gateway;
29pub mod metrics;
30pub(crate) mod packet_policy;
31pub mod state;
32
33/// Start the tunnel gateway.
34///
35/// # Arguments
36/// * `tasks`: The task set used to launch the asynchronous tasks.
37/// * `authz`: The authorization layer for the snaptun.
38/// * `dispatcher_rs`: The receiving end of the dispatcher interface.
39/// * `server_static_secret`: The static secret of the tunnel gateway's tunnel endpoint.
40pub fn start_tunnel_gateway<A, D>(
41 tasks: &mut CancelTaskSet,
42 socket: UdpSocket,
43 authz: Arc<A>,
44 dispatcher: Arc<D>,
45 tun_dispatcher_rx: TunnelGatewayDispatcherReceiver,
46 server_static_secret: x25519_dalek::StaticSecret,
47) where
48 A: SnapTunAuthorization + 'static,
49 D: Dispatcher + 'static,
50{
51 let tun_gateway = TunnelGateway::new(
52 socket,
53 server_static_secret,
54 authz,
55 dispatcher,
56 tun_dispatcher_rx,
57 );
58 let token = tasks.cancellation_token();
59 tasks.spawn_cancellable_task(async move {
60 tun_gateway.start_server(token).await;
61 Ok(())
62 });
63}