2015-12-30 04:52:24 -05:00
|
|
|
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
|
|
|
|
See the README file for details. *)
|
|
|
|
|
2015-12-30 08:48:13 -05:00
|
|
|
open Lwt.Infix
|
2017-03-02 09:52:55 -05:00
|
|
|
open Fw_utils
|
2015-12-30 04:52:24 -05:00
|
|
|
|
2022-03-30 03:12:01 -04:00
|
|
|
module Netback = Netchannel.Backend.Make(Netchannel.Xenstore.Make(Xen_os.Xs))
|
2019-03-17 17:32:17 -04:00
|
|
|
module ClientEth = Ethernet.Make(Netback)
|
2015-12-30 04:52:24 -05:00
|
|
|
|
2016-09-25 10:25:51 -04:00
|
|
|
let src = Logs.Src.create "client_net" ~doc:"Client networking"
|
2015-12-30 08:48:13 -05:00
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
2015-12-30 04:52:24 -05:00
|
|
|
|
2019-03-17 17:32:17 -04:00
|
|
|
let writev eth dst proto fillfn =
|
2016-09-25 10:25:51 -04:00
|
|
|
Lwt.catch
|
2017-03-02 09:52:55 -05:00
|
|
|
(fun () ->
|
2019-03-17 17:32:17 -04:00
|
|
|
ClientEth.write eth dst proto fillfn >|= function
|
2017-03-02 09:52:55 -05:00
|
|
|
| Ok () -> ()
|
|
|
|
| Error e ->
|
2019-03-17 17:32:17 -04:00
|
|
|
Log.err (fun f -> f "error trying to send to client: @[%a@]"
|
|
|
|
ClientEth.pp_error e);
|
2017-03-02 09:52:55 -05:00
|
|
|
)
|
2016-09-25 10:25:51 -04:00
|
|
|
(fun ex ->
|
|
|
|
(* Usually Netback_shutdown, because the client disconnected *)
|
2019-03-17 17:32:17 -04:00
|
|
|
Log.err (fun f -> f "uncaught exception trying to send to client: @[%s@]"
|
|
|
|
(Printexc.to_string ex));
|
2020-01-11 09:39:20 -05:00
|
|
|
Lwt.return_unit
|
2016-09-25 10:25:51 -04:00
|
|
|
)
|
|
|
|
|
2019-05-06 04:54:35 -04:00
|
|
|
class client_iface eth ~domid ~gateway_ip ~client_ip client_mac : client_link =
|
2021-11-05 14:59:00 -04:00
|
|
|
let log_header = Fmt.str "dom%d:%a" domid Ipaddr.V4.pp client_ip in
|
2019-05-06 04:54:35 -04:00
|
|
|
object
|
|
|
|
val queue = FrameQ.create (Ipaddr.V4.to_string client_ip)
|
2020-04-29 09:58:01 -04:00
|
|
|
val mutable rules = []
|
|
|
|
method get_rules = rules
|
|
|
|
method set_rules new_db = rules <- Dao.read_rules new_db client_ip
|
2019-05-06 04:54:35 -04:00
|
|
|
method my_mac = ClientEth.mac eth
|
|
|
|
method other_mac = client_mac
|
|
|
|
method my_ip = gateway_ip
|
|
|
|
method other_ip = client_ip
|
|
|
|
method writev proto fillfn =
|
|
|
|
FrameQ.send queue (fun () ->
|
|
|
|
writev eth client_mac proto fillfn
|
|
|
|
)
|
|
|
|
method log_header = log_header
|
|
|
|
end
|
2015-12-30 08:48:13 -05:00
|
|
|
|
2016-10-01 09:42:27 -04:00
|
|
|
let clients : Cleanup.t Dao.VifMap.t ref = ref Dao.VifMap.empty
|
2015-12-30 08:48:13 -05:00
|
|
|
|
2015-12-31 09:56:24 -05:00
|
|
|
(** Handle an ARP message from the client. *)
|
2017-03-02 09:52:55 -05:00
|
|
|
let input_arp ~fixed_arp ~iface request =
|
2019-03-17 17:32:17 -04:00
|
|
|
match Arp_packet.decode request with
|
2017-03-02 09:52:55 -05:00
|
|
|
| Error e ->
|
2019-03-17 17:32:17 -04:00
|
|
|
Log.warn (fun f -> f "Ignored unknown ARP message: %a" Arp_packet.pp_error e);
|
2020-01-11 09:39:20 -05:00
|
|
|
Lwt.return_unit
|
2017-03-02 09:52:55 -05:00
|
|
|
| Ok arp ->
|
|
|
|
match Client_eth.ARP.input fixed_arp arp with
|
2020-01-11 09:39:20 -05:00
|
|
|
| None -> Lwt.return_unit
|
2017-03-02 09:52:55 -05:00
|
|
|
| Some response ->
|
2019-03-17 17:32:17 -04:00
|
|
|
iface#writev `ARP (fun b -> Arp_packet.encode_into response b; Arp_packet.size)
|
2015-12-31 09:56:24 -05:00
|
|
|
|
|
|
|
(** Handle an IPv4 packet from the client. *)
|
2020-04-29 10:06:48 -04:00
|
|
|
let input_ipv4 get_ts cache ~iface ~router dns_client packet =
|
2020-02-08 09:58:37 -05:00
|
|
|
let cache', r = Nat_packet.of_ipv4_packet !cache ~now:(get_ts ()) packet in
|
|
|
|
cache := cache';
|
|
|
|
match r with
|
2017-03-02 09:52:55 -05:00
|
|
|
| Error e ->
|
2017-03-05 11:31:04 -05:00
|
|
|
Log.warn (fun f -> f "Ignored unknown IPv4 message: %a" Nat_packet.pp_error e);
|
2020-01-11 09:39:20 -05:00
|
|
|
Lwt.return_unit
|
|
|
|
| Ok None -> Lwt.return_unit
|
2020-01-11 09:36:02 -05:00
|
|
|
| Ok (Some packet) ->
|
2017-03-05 11:31:04 -05:00
|
|
|
let `IPv4 (ip, _) = packet in
|
2017-03-02 09:52:55 -05:00
|
|
|
let src = ip.Ipv4_packet.src in
|
2020-04-29 10:06:48 -04:00
|
|
|
if src = iface#other_ip then Firewall.ipv4_from_client dns_client router ~src:iface packet
|
2017-03-02 09:52:55 -05:00
|
|
|
else (
|
|
|
|
Log.warn (fun f -> f "Incorrect source IP %a in IP packet from %a (dropping)"
|
2019-04-17 06:03:17 -04:00
|
|
|
Ipaddr.V4.pp src Ipaddr.V4.pp iface#other_ip);
|
2020-01-11 09:39:20 -05:00
|
|
|
Lwt.return_unit
|
2017-03-02 09:52:55 -05:00
|
|
|
)
|
2015-12-31 09:56:24 -05:00
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
(** Connect to a new client's interface and listen for incoming frames and firewall rule changes. *)
|
2020-04-29 10:06:48 -04:00
|
|
|
let add_vif get_ts { Dao.ClientVif.domid; device_id } dns_client ~client_ip ~router ~cleanup_tasks qubesDB =
|
2015-12-31 09:56:24 -05:00
|
|
|
Netback.make ~domid ~device_id >>= fun backend ->
|
2016-01-08 06:31:27 -05:00
|
|
|
Log.info (fun f -> f "Client %d (IP: %s) ready" domid (Ipaddr.V4.to_string client_ip));
|
2017-03-02 09:52:55 -05:00
|
|
|
ClientEth.connect backend >>= fun eth ->
|
2019-05-01 05:05:14 -04:00
|
|
|
let client_mac = Netback.frontend_mac backend in
|
2016-01-01 06:32:57 -05:00
|
|
|
let client_eth = router.Router.client_eth in
|
|
|
|
let gateway_ip = Client_eth.client_gw client_eth in
|
2019-05-06 04:54:35 -04:00
|
|
|
let iface = new client_iface eth ~domid ~gateway_ip ~client_ip client_mac in
|
2020-04-29 09:58:01 -04:00
|
|
|
(* update the rules whenever QubesDB notices a change for this IP *)
|
|
|
|
let qubesdb_updater =
|
|
|
|
Lwt.catch
|
|
|
|
(fun () ->
|
|
|
|
let rec update current_db current_rules =
|
|
|
|
Qubes.DB.got_new_commit qubesDB (Dao.db_root client_ip) current_db >>= fun new_db ->
|
|
|
|
iface#set_rules new_db;
|
|
|
|
let new_rules = iface#get_rules in
|
|
|
|
(if current_rules = new_rules then
|
|
|
|
Log.debug (fun m -> m "Rules did not change for %s" (Ipaddr.V4.to_string client_ip))
|
|
|
|
else begin
|
|
|
|
Log.debug (fun m -> m "New firewall rules for %s@.%a"
|
|
|
|
(Ipaddr.V4.to_string client_ip)
|
2021-11-05 14:59:00 -04:00
|
|
|
Fmt.(list ~sep:(any "@.") Pf_qubes.Parse_qubes.pp_rule) new_rules);
|
2020-04-29 09:58:01 -04:00
|
|
|
(* empty NAT table if rules are updated: they might deny old connections *)
|
2020-04-29 10:06:48 -04:00
|
|
|
My_nat.remove_connections router.Router.nat router.Router.ports client_ip;
|
2020-04-29 09:58:01 -04:00
|
|
|
end);
|
|
|
|
update new_db new_rules
|
|
|
|
in
|
|
|
|
update Qubes.DB.KeyMap.empty [])
|
|
|
|
(function Lwt.Canceled -> Lwt.return_unit | e -> Lwt.fail e)
|
|
|
|
in
|
|
|
|
Cleanup.on_cleanup cleanup_tasks (fun () -> Lwt.cancel qubesdb_updater);
|
2016-09-25 10:14:16 -04:00
|
|
|
Router.add_client router iface >>= fun () ->
|
2015-12-31 09:56:24 -05:00
|
|
|
Cleanup.on_cleanup cleanup_tasks (fun () -> Router.remove_client router iface);
|
2016-01-01 06:32:57 -05:00
|
|
|
let fixed_arp = Client_eth.ARP.create ~net:client_eth iface in
|
2020-02-08 09:58:37 -05:00
|
|
|
let fragment_cache = ref (Fragments.Cache.empty (256 * 1024)) in
|
2020-04-29 09:58:01 -04:00
|
|
|
let listener =
|
|
|
|
Lwt.catch
|
|
|
|
(fun () ->
|
2022-01-09 06:36:35 -05:00
|
|
|
Netback.listen backend ~header_size:Ethernet.Packet.sizeof_ethernet (fun frame ->
|
|
|
|
match Ethernet.Packet.of_cstruct frame with
|
2020-04-29 09:58:01 -04:00
|
|
|
| Error err -> Log.warn (fun f -> f "Invalid Ethernet frame: %s" err); Lwt.return_unit
|
|
|
|
| Ok (eth, payload) ->
|
2022-01-09 06:36:35 -05:00
|
|
|
match eth.Ethernet.Packet.ethertype with
|
2020-04-29 09:58:01 -04:00
|
|
|
| `ARP -> input_arp ~fixed_arp ~iface payload
|
2020-04-29 10:06:48 -04:00
|
|
|
| `IPv4 -> input_ipv4 get_ts fragment_cache ~iface ~router dns_client payload
|
2020-04-29 09:58:01 -04:00
|
|
|
| `IPv6 -> Lwt.return_unit (* TODO: oh no! *)
|
|
|
|
)
|
|
|
|
>|= or_raise "Listen on client interface" Netback.pp_error)
|
|
|
|
(function Lwt.Canceled -> Lwt.return_unit | e -> Lwt.fail e)
|
|
|
|
in
|
|
|
|
Cleanup.on_cleanup cleanup_tasks (fun () -> Lwt.cancel listener);
|
|
|
|
Lwt.pick [ qubesdb_updater ; listener ]
|
2015-12-31 09:56:24 -05:00
|
|
|
|
|
|
|
(** A new client VM has been found in XenStore. Find its interface and connect to it. *)
|
2020-04-29 10:06:48 -04:00
|
|
|
let add_client get_ts dns_client ~router vif client_ip qubesDB =
|
2015-12-30 08:48:13 -05:00
|
|
|
let cleanup_tasks = Cleanup.create () in
|
2020-04-29 09:58:01 -04:00
|
|
|
Log.info (fun f -> f "add client vif %a with IP %a"
|
|
|
|
Dao.ClientVif.pp vif Ipaddr.V4.pp client_ip);
|
2015-12-30 08:48:13 -05:00
|
|
|
Lwt.async (fun () ->
|
2016-10-01 09:42:27 -04:00
|
|
|
Lwt.catch (fun () ->
|
2020-04-29 10:06:48 -04:00
|
|
|
add_vif get_ts vif dns_client ~client_ip ~router ~cleanup_tasks qubesDB
|
2016-10-01 09:42:27 -04:00
|
|
|
)
|
|
|
|
(fun ex ->
|
2017-11-09 10:20:55 -05:00
|
|
|
Log.warn (fun f -> f "Error with client %a: %s"
|
2016-10-01 09:42:27 -04:00
|
|
|
Dao.ClientVif.pp vif (Printexc.to_string ex));
|
2020-01-11 09:39:20 -05:00
|
|
|
Lwt.return_unit
|
2016-10-01 09:42:27 -04:00
|
|
|
)
|
|
|
|
);
|
2015-12-30 08:48:13 -05:00
|
|
|
cleanup_tasks
|
|
|
|
|
2015-12-31 09:56:24 -05:00
|
|
|
(** Watch XenStore for notifications of new clients. *)
|
2020-04-29 10:06:48 -04:00
|
|
|
let listen get_ts dns_client qubesDB router =
|
2015-12-30 08:48:13 -05:00
|
|
|
Dao.watch_clients (fun new_set ->
|
|
|
|
(* Check for removed clients *)
|
2016-10-01 09:42:27 -04:00
|
|
|
!clients |> Dao.VifMap.iter (fun key cleanup ->
|
|
|
|
if not (Dao.VifMap.mem key new_set) then (
|
|
|
|
clients := !clients |> Dao.VifMap.remove key;
|
|
|
|
Log.info (fun f -> f "client %a has gone" Dao.ClientVif.pp key);
|
2015-12-30 08:48:13 -05:00
|
|
|
Cleanup.cleanup cleanup
|
|
|
|
)
|
|
|
|
);
|
|
|
|
(* Check for added clients *)
|
2016-10-01 09:42:27 -04:00
|
|
|
new_set |> Dao.VifMap.iter (fun key ip_addr ->
|
|
|
|
if not (Dao.VifMap.mem key !clients) then (
|
2020-04-29 10:06:48 -04:00
|
|
|
let cleanup = add_client get_ts dns_client ~router key ip_addr qubesDB in
|
2020-04-29 09:58:01 -04:00
|
|
|
Log.debug (fun f -> f "client %a arrived" Dao.ClientVif.pp key);
|
2016-10-01 09:42:27 -04:00
|
|
|
clients := !clients |> Dao.VifMap.add key cleanup
|
2015-12-30 08:48:13 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|