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 = {
|
2015-12-30 08:48:13 -05:00
|
|
|
client_eth : Client_eth.t;
|
2017-03-02 09:52:55 -05:00
|
|
|
nat : My_nat.t;
|
2016-01-01 06:32:57 -05:00
|
|
|
uplink : interface;
|
2020-04-29 10:06:48 -04:00
|
|
|
(* NOTE: do not try to make this pure, it relies on mvars / side effects *)
|
|
|
|
ports : My_nat.ports;
|
2015-12-30 04:52:24 -05:00
|
|
|
}
|
|
|
|
|
2017-03-02 09:52:55 -05:00
|
|
|
let create ~client_eth ~uplink ~nat =
|
2020-04-29 10:06:48 -04:00
|
|
|
let ports = My_nat.empty_ports () in
|
|
|
|
{ client_eth; nat; uplink; ports }
|
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
|
2016-09-25 09:38:17 -04:00
|
|
|
match Client_eth.lookup t.client_eth dst_ip with
|
|
|
|
| Some client_link -> Some (client_link :> interface)
|
|
|
|
| None -> Some t.uplink
|
2015-12-30 04:52:24 -05:00
|
|
|
|
2015-12-30 08:48:13 -05:00
|
|
|
let add_client t = Client_eth.add_client t.client_eth
|
|
|
|
let remove_client t = Client_eth.remove_client t.client_eth
|
|
|
|
|
2015-12-30 11:07:16 -05:00
|
|
|
let classify t ip =
|
2019-05-16 14:18:31 -04:00
|
|
|
if ip = Ipaddr.V4 t.uplink#my_ip then `Firewall
|
2016-01-01 06:32:57 -05:00
|
|
|
else if ip = Ipaddr.V4 t.uplink#other_ip then `NetVM
|
2015-12-30 11:07:16 -05:00
|
|
|
else (Client_eth.classify t.client_eth ip :> Packet.host)
|
2016-01-01 06:32:57 -05:00
|
|
|
|
|
|
|
let resolve t = function
|
2019-05-16 14:18:31 -04:00
|
|
|
| `Firewall -> Ipaddr.V4 t.uplink#my_ip
|
2016-01-01 06:32:57 -05:00
|
|
|
| `NetVM -> Ipaddr.V4 t.uplink#other_ip
|
|
|
|
| #Client_eth.host as host -> Client_eth.resolve t.client_eth host
|