2015-12-30 09:52:24 +00:00
|
|
|
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
|
|
|
|
See the README file for details. *)
|
|
|
|
|
2017-03-02 14:52:55 +00:00
|
|
|
open Fw_utils
|
2015-12-30 09:52:24 +00:00
|
|
|
|
2015-12-30 16:07:16 +00:00
|
|
|
(* The routing table *)
|
2023-07-01 11:56:14 +02:00
|
|
|
let src = Logs.Src.create "router" ~doc:"Packet router"
|
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
2015-12-30 16:07:16 +00:00
|
|
|
|
2015-12-30 09:52:24 +00:00
|
|
|
type t = {
|
2023-06-30 15:33:41 +02:00
|
|
|
config : Dao.network_config;
|
2023-06-30 13:59:03 +02:00
|
|
|
clients : Client_eth.t;
|
2017-03-02 14:52:55 +00:00
|
|
|
nat : My_nat.t;
|
2023-06-30 16:58:08 +02:00
|
|
|
uplink : interface option;
|
2015-12-30 09:52:24 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 16:58:08 +02:00
|
|
|
let create ~config ~clients ~nat ?uplink =
|
2023-06-30 15:33:41 +02:00
|
|
|
{ config; clients; nat; uplink }
|
2015-12-30 09:52:24 +00:00
|
|
|
|
|
|
|
let target t buf =
|
2017-03-02 14:52:55 +00:00
|
|
|
let dst_ip = buf.Ipv4_packet.dst in
|
2023-06-30 13:59:03 +02:00
|
|
|
match Client_eth.lookup t.clients dst_ip with
|
2016-09-25 14:38:17 +01:00
|
|
|
| Some client_link -> Some (client_link :> interface)
|
2023-07-01 11:56:14 +02:00
|
|
|
| None -> begin match t.uplink with
|
|
|
|
| None -> (
|
|
|
|
match Client_eth.lookup t.clients t.config.netvm_ip with
|
|
|
|
| Some uplink -> Some (uplink :> interface)
|
|
|
|
| None -> None
|
|
|
|
)
|
|
|
|
| uplink -> uplink
|
|
|
|
end
|
2015-12-30 09:52:24 +00:00
|
|
|
|
2023-06-30 13:59:03 +02:00
|
|
|
let add_client t = Client_eth.add_client t.clients
|
|
|
|
let remove_client t = Client_eth.remove_client t.clients
|
2015-12-30 13:48:13 +00:00
|
|
|
|
2015-12-30 16:07:16 +00:00
|
|
|
let classify t ip =
|
2023-06-30 15:33:41 +02: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 13:59:03 +02:00
|
|
|
else (Client_eth.classify t.clients ip :> Packet.host)
|
2016-01-01 11:32:57 +00:00
|
|
|
|
|
|
|
let resolve t = function
|
2023-06-30 15:33:41 +02:00
|
|
|
| `Firewall -> Ipaddr.V4 t.config.our_ip
|
|
|
|
| `NetVM -> Ipaddr.V4 t.config.netvm_ip
|
2023-06-30 13:59:03 +02:00
|
|
|
| #Client_eth.host as host -> Client_eth.resolve t.clients host
|