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
|
|
|
|
2015-12-30 11:07:16 -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;
|
2023-06-30 07:59:03 -04:00
|
|
|
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
|
2023-06-30 07:59:03 -04:00
|
|
|
match Client_eth.lookup t.clients dst_ip with
|
2016-09-25 09:38:17 -04:00
|
|
|
| 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
|
|
|
|
2023-06-30 07:59:03 -04:00
|
|
|
let add_client t = Client_eth.add_client t.clients
|
|
|
|
let remove_client t = Client_eth.remove_client t.clients
|
2015-12-30 08:48:13 -05:00
|
|
|
|
2015-12-30 11:07:16 -05:00
|
|
|
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
|
2023-06-30 07:59:03 -04:00
|
|
|
else (Client_eth.classify t.clients ip :> Packet.host)
|
2016-01-01 06:32:57 -05:00
|
|
|
|
|
|
|
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
|
2023-06-30 07:59:03 -04:00
|
|
|
| #Client_eth.host as host -> Client_eth.resolve t.clients host
|