2016-09-25 09:38:17 -04:00
|
|
|
(* Copyright (C) 2016, Thomas Leonard <thomas.leonard@unikernel.com>
|
2015-12-30 08:48:13 -05:00
|
|
|
See the README file for details. *)
|
|
|
|
|
2017-03-02 09:52:55 -05:00
|
|
|
open Fw_utils
|
2016-09-25 10:14:16 -04:00
|
|
|
open Lwt.Infix
|
2015-12-30 08:48:13 -05:00
|
|
|
|
2016-09-25 09:38:17 -04:00
|
|
|
let src = Logs.Src.create "client_eth" ~doc:"Ethernet networks for NetVM clients"
|
2015-12-30 08:48:13 -05:00
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
|
|
|
|
|
|
|
type t = {
|
|
|
|
mutable iface_of_ip : client_link IpMap.t;
|
2016-09-25 10:14:16 -04:00
|
|
|
changed : unit Lwt_condition.t; (* Fires when [iface_of_ip] changes. *)
|
2015-12-30 08:48:13 -05:00
|
|
|
client_gw : Ipaddr.V4.t; (* The IP that clients are given as their default gateway. *)
|
|
|
|
}
|
|
|
|
|
2016-01-01 06:32:57 -05:00
|
|
|
type host =
|
|
|
|
[ `Client of client_link
|
2019-05-16 14:18:31 -04:00
|
|
|
| `Firewall
|
2016-01-01 06:32:57 -05:00
|
|
|
| `External of Ipaddr.t ]
|
|
|
|
|
2016-09-25 09:38:17 -04:00
|
|
|
let create ~client_gw =
|
2016-09-25 10:14:16 -04:00
|
|
|
let changed = Lwt_condition.create () in
|
|
|
|
{ iface_of_ip = IpMap.empty; client_gw; changed }
|
2015-12-30 08:48:13 -05:00
|
|
|
|
2016-01-01 06:32:57 -05:00
|
|
|
let client_gw t = t.client_gw
|
2015-12-30 08:48:13 -05:00
|
|
|
|
|
|
|
let add_client t iface =
|
2015-12-30 11:07:16 -05:00
|
|
|
let ip = iface#other_ip in
|
2016-09-25 10:14:16 -04:00
|
|
|
let rec aux () =
|
2019-05-06 04:54:35 -04:00
|
|
|
match IpMap.find ip t.iface_of_ip with
|
|
|
|
| Some old ->
|
2016-09-25 10:14:16 -04:00
|
|
|
(* Wait for old client to disappear before adding one with the same IP address.
|
|
|
|
Otherwise, its [remove_client] call will remove the new client instead. *)
|
2019-05-06 04:54:35 -04:00
|
|
|
Log.info (fun f -> f ~header:iface#log_header "Waiting for old client %s to go away before accepting new one" old#log_header);
|
2016-09-25 10:14:16 -04:00
|
|
|
Lwt_condition.wait t.changed >>= aux
|
2019-05-06 04:54:35 -04:00
|
|
|
| None ->
|
2016-09-25 10:14:16 -04:00
|
|
|
t.iface_of_ip <- t.iface_of_ip |> IpMap.add ip iface;
|
|
|
|
Lwt_condition.broadcast t.changed ();
|
|
|
|
Lwt.return_unit
|
|
|
|
in
|
|
|
|
aux ()
|
2015-12-30 08:48:13 -05:00
|
|
|
|
|
|
|
let remove_client t iface =
|
2015-12-30 11:07:16 -05:00
|
|
|
let ip = iface#other_ip in
|
2015-12-30 08:48:13 -05:00
|
|
|
assert (IpMap.mem ip t.iface_of_ip);
|
2016-09-25 10:14:16 -04:00
|
|
|
t.iface_of_ip <- t.iface_of_ip |> IpMap.remove ip;
|
|
|
|
Lwt_condition.broadcast t.changed ()
|
2015-12-30 08:48:13 -05:00
|
|
|
|
|
|
|
let lookup t ip = IpMap.find ip t.iface_of_ip
|
|
|
|
|
2015-12-31 04:56:58 -05:00
|
|
|
let classify t ip =
|
|
|
|
match ip with
|
|
|
|
| Ipaddr.V6 _ -> `External ip
|
|
|
|
| Ipaddr.V4 ip4 ->
|
2019-05-16 14:18:31 -04:00
|
|
|
if ip4 = t.client_gw then `Firewall
|
2017-03-02 09:52:55 -05:00
|
|
|
else match lookup t ip4 with
|
|
|
|
| Some client_link -> `Client client_link
|
|
|
|
| None -> `External ip
|
2015-12-30 11:07:16 -05:00
|
|
|
|
2016-01-01 06:32:57 -05:00
|
|
|
let resolve t : host -> Ipaddr.t = function
|
|
|
|
| `Client client_link -> Ipaddr.V4 client_link#other_ip
|
2019-05-16 14:18:31 -04:00
|
|
|
| `Firewall -> Ipaddr.V4 t.client_gw
|
2016-01-01 06:32:57 -05:00
|
|
|
| `External addr -> addr
|
|
|
|
|
2015-12-30 08:48:13 -05:00
|
|
|
module ARP = struct
|
|
|
|
type arp = {
|
|
|
|
net : t;
|
|
|
|
client_link : client_link;
|
|
|
|
}
|
|
|
|
|
|
|
|
let lookup t ip =
|
2016-01-01 05:55:34 -05:00
|
|
|
if ip = t.net.client_gw then Some t.client_link#my_mac
|
2019-05-05 12:26:56 -04:00
|
|
|
else if (Ipaddr.V4.to_bytes ip).[3] = '\x01' then (
|
|
|
|
Log.info (fun f -> f ~header:t.client_link#log_header
|
|
|
|
"Request for %a is invalid, but pretending it's me (see Qubes issue #5022)" Ipaddr.V4.pp ip);
|
|
|
|
Some t.client_link#my_mac
|
|
|
|
) else None
|
2017-03-02 09:52:55 -05:00
|
|
|
(* We're now treating client networks as point-to-point links,
|
|
|
|
so we no longer respond on behalf of other clients. *)
|
2016-09-25 09:38:17 -04:00
|
|
|
(*
|
2015-12-30 08:48:13 -05:00
|
|
|
else match IpMap.find ip t.net.iface_of_ip with
|
2015-12-30 11:07:16 -05:00
|
|
|
| Some client_iface -> Some client_iface#other_mac
|
2015-12-30 08:48:13 -05:00
|
|
|
| None -> None
|
2016-09-25 09:38:17 -04:00
|
|
|
*)
|
2015-12-30 08:48:13 -05:00
|
|
|
|
|
|
|
let create ~net client_link = {net; client_link}
|
|
|
|
|
2017-03-02 09:52:55 -05:00
|
|
|
let input_query t arp =
|
2019-03-17 17:32:17 -04:00
|
|
|
let req_ipv4 = arp.Arp_packet.target_ip in
|
2019-05-06 04:54:35 -04:00
|
|
|
let pf (f : ?header:string -> ?tags:_ -> _) fmt =
|
|
|
|
f ~header:t.client_link#log_header ("who-has %a? " ^^ fmt) Ipaddr.V4.pp req_ipv4
|
|
|
|
in
|
2016-01-01 05:55:34 -05:00
|
|
|
if req_ipv4 = t.client_link#other_ip then (
|
2019-05-06 04:54:35 -04:00
|
|
|
Log.info (fun f -> pf f "ignoring request for client's own IP");
|
2015-12-30 08:48:13 -05:00
|
|
|
None
|
|
|
|
) else match lookup t req_ipv4 with
|
2017-03-02 09:52:55 -05:00
|
|
|
| None ->
|
2019-05-06 04:54:35 -04:00
|
|
|
Log.info (fun f -> pf f "unknown address; not responding");
|
2015-12-30 08:48:13 -05:00
|
|
|
None
|
2017-03-02 09:52:55 -05:00
|
|
|
| Some req_mac ->
|
2019-05-06 04:54:35 -04:00
|
|
|
Log.info (fun f -> pf f "responding with %a" Macaddr.pp req_mac);
|
2019-03-17 17:32:17 -04:00
|
|
|
Some { Arp_packet.
|
|
|
|
operation = Arp_packet.Reply;
|
2017-03-02 09:52:55 -05:00
|
|
|
(* The Target Hardware Address and IP are copied from the request *)
|
2019-03-17 17:32:17 -04:00
|
|
|
target_ip = arp.Arp_packet.source_ip;
|
|
|
|
target_mac = arp.Arp_packet.source_mac;
|
|
|
|
source_ip = req_ipv4;
|
|
|
|
source_mac = req_mac;
|
2017-03-02 09:52:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let input_gratuitous t arp =
|
2019-03-17 17:32:17 -04:00
|
|
|
let source_ip = arp.Arp_packet.source_ip in
|
|
|
|
let source_mac = arp.Arp_packet.source_mac in
|
2019-05-06 04:54:35 -04:00
|
|
|
let header = t.client_link#log_header in
|
2019-03-17 17:32:17 -04:00
|
|
|
match lookup t source_ip with
|
|
|
|
| Some real_mac when Macaddr.compare source_mac real_mac = 0 ->
|
2019-05-06 04:54:35 -04:00
|
|
|
Log.info (fun f -> f ~header "client suggests updating %s -> %s (as expected)"
|
2019-03-17 17:32:17 -04:00
|
|
|
(Ipaddr.V4.to_string source_ip) (Macaddr.to_string source_mac));
|
2015-12-30 08:48:13 -05:00
|
|
|
| Some other_mac ->
|
2019-05-06 04:54:35 -04:00
|
|
|
Log.warn (fun f -> f ~header "client suggests incorrect update %s -> %s (should be %s)"
|
2019-03-17 17:32:17 -04:00
|
|
|
(Ipaddr.V4.to_string source_ip) (Macaddr.to_string source_mac) (Macaddr.to_string other_mac));
|
2015-12-30 08:48:13 -05:00
|
|
|
| None ->
|
2019-05-06 04:54:35 -04:00
|
|
|
Log.warn (fun f -> f ~header "client suggests incorrect update %s -> %s (unexpected IP)"
|
2019-03-17 17:32:17 -04:00
|
|
|
(Ipaddr.V4.to_string source_ip) (Macaddr.to_string source_mac))
|
2017-03-02 09:52:55 -05:00
|
|
|
|
|
|
|
let input t arp =
|
2019-03-17 17:32:17 -04:00
|
|
|
let op = arp.Arp_packet.operation in
|
2017-03-02 09:52:55 -05:00
|
|
|
match op with
|
2019-03-17 17:32:17 -04:00
|
|
|
| Arp_packet.Request -> input_query t arp
|
|
|
|
| Arp_packet.Reply -> input_gratuitous t arp; None
|
2015-12-30 08:48:13 -05:00
|
|
|
end
|