2015-12-30 08:59:13 -05:00
|
|
|
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
|
|
|
|
See the README file for details. *)
|
|
|
|
|
|
|
|
open Lwt.Infix
|
2017-03-02 09:52:55 -05:00
|
|
|
open Fw_utils
|
2015-12-30 08:59:13 -05:00
|
|
|
|
2019-03-17 17:32:17 -04:00
|
|
|
module Eth = Ethernet.Make(Netif)
|
2015-12-30 08:59:13 -05:00
|
|
|
|
|
|
|
let src = Logs.Src.create "uplink" ~doc:"Network connection to NetVM"
|
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
|
|
|
|
2022-03-30 03:12:01 -04:00
|
|
|
module Make (R:Mirage_random.S) (Clock : Mirage_clock.MCLOCK) (Time : Mirage_time.S) = struct
|
|
|
|
module Arp = Arp.Make(Eth)(Time)
|
2020-04-29 10:06:48 -04:00
|
|
|
module I = Static_ipv4.Make(R)(Clock)(Eth)(Arp)
|
|
|
|
module U = Udp.Make(I)(R)
|
2015-12-30 08:59:13 -05:00
|
|
|
|
2020-04-29 10:06:48 -04:00
|
|
|
type t = {
|
|
|
|
net : Netif.t;
|
|
|
|
eth : Eth.t;
|
|
|
|
arp : Arp.t;
|
|
|
|
interface : interface;
|
|
|
|
mutable fragments : Fragments.Cache.t;
|
|
|
|
ip : I.t;
|
|
|
|
udp: U.t;
|
|
|
|
}
|
2015-12-30 08:59:13 -05:00
|
|
|
|
2020-01-13 04:50:48 -05:00
|
|
|
class netvm_iface eth mac ~my_ip ~other_ip : interface = object
|
|
|
|
method my_mac = Eth.mac eth
|
|
|
|
method my_ip = my_ip
|
|
|
|
method other_ip = other_ip
|
|
|
|
method writev ethertype fillfn =
|
2022-10-06 12:06:02 -04:00
|
|
|
mac >>= fun dst ->
|
|
|
|
Eth.write eth dst ethertype fillfn >|= or_raise "Write to uplink" Eth.pp_error
|
2020-01-13 04:50:48 -05:00
|
|
|
end
|
2015-12-30 08:59:13 -05:00
|
|
|
|
2020-04-29 10:06:48 -04:00
|
|
|
let send_dns_client_query t ~src_port ~dst ~dst_port buf =
|
|
|
|
U.write ~src_port ~dst ~dst_port t.udp buf >|= function
|
|
|
|
| Error s -> Log.err (fun f -> f "error sending udp packet: %a" U.pp_error s); Error (`Msg "failure")
|
|
|
|
| Ok () -> Ok ()
|
|
|
|
|
|
|
|
let listen t get_ts dns_responses router =
|
|
|
|
let handle_packet ip_header ip_packet =
|
|
|
|
let open Udp_packet in
|
|
|
|
|
|
|
|
Log.debug (fun f -> f "received ipv4 packet from %a on uplink" Ipaddr.V4.pp ip_header.Ipv4_packet.src);
|
|
|
|
match ip_packet with
|
2022-10-08 04:50:29 -04:00
|
|
|
| `UDP (header, packet) when My_nat.dns_port router.Router.nat header.dst_port ->
|
2020-04-29 10:06:48 -04:00
|
|
|
Log.debug (fun f -> f "found a DNS packet whose dst_port (%d) was in the list of dns_client ports" header.dst_port);
|
|
|
|
Lwt_mvar.put dns_responses (header, packet)
|
|
|
|
| _ ->
|
|
|
|
Firewall.ipv4_from_netvm router (`IPv4 (ip_header, ip_packet))
|
|
|
|
in
|
2022-01-09 06:36:35 -05:00
|
|
|
Netif.listen t.net ~header_size:Ethernet.Packet.sizeof_ethernet (fun frame ->
|
2020-04-29 10:06:48 -04:00
|
|
|
(* Handle one Ethernet frame from NetVM *)
|
|
|
|
Eth.input t.eth
|
2020-01-13 04:50:48 -05:00
|
|
|
~arpv4:(Arp.input t.arp)
|
|
|
|
~ipv4:(fun ip ->
|
2020-02-08 09:58:37 -05:00
|
|
|
let cache, r =
|
|
|
|
Nat_packet.of_ipv4_packet t.fragments ~now:(get_ts ()) ip
|
|
|
|
in
|
|
|
|
t.fragments <- cache;
|
|
|
|
match r with
|
2020-04-29 10:06:48 -04:00
|
|
|
| Error e ->
|
|
|
|
Log.warn (fun f -> f "Ignored unknown IPv4 message from uplink: %a" Nat_packet.pp_error e);
|
|
|
|
Lwt.return ()
|
|
|
|
| Ok None -> Lwt.return_unit
|
|
|
|
| Ok (Some (`IPv4 (header, packet))) -> handle_packet header packet
|
|
|
|
)
|
2020-01-13 04:50:48 -05:00
|
|
|
~ipv6:(fun _ip -> Lwt.return_unit)
|
|
|
|
frame
|
|
|
|
) >|= or_raise "Uplink listen loop" Netif.pp_error
|
2015-12-30 08:59:13 -05:00
|
|
|
|
2020-04-29 10:06:48 -04:00
|
|
|
|
2020-01-13 04:50:48 -05:00
|
|
|
let interface t = t.interface
|
2015-12-30 08:59:13 -05:00
|
|
|
|
2020-01-13 04:50:48 -05:00
|
|
|
let connect config =
|
2020-04-29 10:06:48 -04:00
|
|
|
let my_ip = config.Dao.uplink_our_ip in
|
|
|
|
let gateway = config.Dao.uplink_netvm_ip in
|
2020-01-13 04:50:48 -05:00
|
|
|
Netif.connect "0" >>= fun net ->
|
|
|
|
Eth.connect net >>= fun eth ->
|
|
|
|
Arp.connect eth >>= fun arp ->
|
2020-04-29 10:06:48 -04:00
|
|
|
Arp.add_ip arp my_ip >>= fun () ->
|
2020-07-03 10:39:06 -04:00
|
|
|
let cidr = Ipaddr.V4.Prefix.make 0 my_ip in
|
|
|
|
I.connect ~cidr ~gateway eth arp >>= fun ip ->
|
2020-04-29 10:06:48 -04:00
|
|
|
U.connect ip >>= fun udp ->
|
2020-01-13 04:50:48 -05:00
|
|
|
let netvm_mac =
|
2020-04-29 10:06:48 -04:00
|
|
|
Arp.query arp gateway
|
2020-01-13 04:50:48 -05:00
|
|
|
>|= or_raise "Getting MAC of our NetVM" Arp.pp_error in
|
|
|
|
let interface = new netvm_iface eth netvm_mac
|
2020-04-29 10:06:48 -04:00
|
|
|
~my_ip
|
2020-01-13 04:50:48 -05:00
|
|
|
~other_ip:config.Dao.uplink_netvm_ip in
|
2020-02-08 09:58:37 -05:00
|
|
|
let fragments = Fragments.Cache.empty (256 * 1024) in
|
2020-04-29 10:06:48 -04:00
|
|
|
Lwt.return { net; eth; arp; interface ; fragments ; ip ; udp }
|
|
|
|
end
|