qubes-mirage-firewall/router.ml

36 lines
998 B
OCaml
Raw Normal View History

2015-12-30 04:52:24 -05:00
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
See the README file for details. *)
2017-03-02 09:52:55 -05:00
open Fw_utils
2015-12-30 04:52:24 -05:00
(* The routing table *)
2015-12-30 04:52:24 -05:00
type t = {
2023-06-30 09:33:41 -04:00
config : Dao.network_config;
clients : Client_eth.t;
2017-03-02 09:52:55 -05:00
nat : My_nat.t;
2023-06-30 10:58:08 -04:00
uplink : interface option;
2015-12-30 04:52:24 -05:00
}
2023-06-30 10:58:08 -04:00
let create ~config ~clients ~nat ?uplink =
2023-06-30 09:33:41 -04:00
{ config; clients; nat; uplink }
2015-12-30 04:52:24 -05:00
let target t buf =
2017-03-02 09:52:55 -05:00
let dst_ip = buf.Ipv4_packet.dst in
match Client_eth.lookup t.clients dst_ip with
| Some client_link -> Some (client_link :> interface)
2023-06-30 10:58:08 -04:00
| None -> t.uplink
2015-12-30 04:52:24 -05:00
let add_client t = Client_eth.add_client t.clients
let remove_client t = Client_eth.remove_client t.clients
let classify t ip =
2023-06-30 09:33:41 -04:00
if ip = Ipaddr.V4 t.config.our_ip then `Firewall
else if ip = Ipaddr.V4 t.config.netvm_ip then `NetVM
else (Client_eth.classify t.clients ip :> Packet.host)
let resolve t = function
2023-06-30 09:33:41 -04:00
| `Firewall -> Ipaddr.V4 t.config.our_ip
| `NetVM -> Ipaddr.V4 t.config.netvm_ip
| #Client_eth.host as host -> Client_eth.resolve t.clients host