2015-12-30 11:07:16 -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 11:07:16 -05:00
|
|
|
open Packet
|
2017-03-02 09:52:55 -05:00
|
|
|
open Lwt.Infix
|
2015-12-30 11:07:16 -05:00
|
|
|
|
|
|
|
let src = Logs.Src.create "firewall" ~doc:"Packet handler"
|
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
|
|
|
|
|
|
|
(* Transmission *)
|
|
|
|
|
2017-03-05 11:31:04 -05:00
|
|
|
let transmit_ipv4 packet iface =
|
2016-06-26 07:04:47 -04:00
|
|
|
Lwt.catch
|
2017-03-06 09:30:41 -05:00
|
|
|
(fun () ->
|
|
|
|
Lwt.catch
|
2019-03-17 17:32:17 -04:00
|
|
|
(fun () ->
|
|
|
|
iface#writev `IPv4 (fun b ->
|
|
|
|
match Nat_packet.into_cstruct packet b with
|
|
|
|
| Error e ->
|
|
|
|
Log.warn (fun f -> f "Failed to write packet to %a: %a"
|
|
|
|
Ipaddr.V4.pp iface#other_ip
|
|
|
|
Nat_packet.pp_error e);
|
|
|
|
0
|
|
|
|
| Ok n -> n
|
|
|
|
)
|
|
|
|
)
|
2017-03-06 09:30:41 -05:00
|
|
|
(fun ex ->
|
|
|
|
Log.warn (fun f -> f "Failed to write packet to %a: %s"
|
2019-02-01 04:25:29 -05:00
|
|
|
Ipaddr.V4.pp iface#other_ip
|
2017-03-06 09:30:41 -05:00
|
|
|
(Printexc.to_string ex));
|
|
|
|
Lwt.return ()
|
|
|
|
)
|
|
|
|
)
|
2016-06-26 07:04:47 -04:00
|
|
|
(fun ex ->
|
2017-03-06 09:30:41 -05:00
|
|
|
Log.err (fun f -> f "Exception in transmit_ipv4: %s for:@.%a"
|
|
|
|
(Printexc.to_string ex)
|
|
|
|
Nat_packet.pp packet
|
|
|
|
);
|
2016-06-26 07:04:47 -04:00
|
|
|
Lwt.return ()
|
|
|
|
)
|
2015-12-30 11:07:16 -05:00
|
|
|
|
2017-03-05 11:31:04 -05:00
|
|
|
let forward_ipv4 t packet =
|
|
|
|
let `IPv4 (ip, _) = packet in
|
2017-03-02 09:52:55 -05:00
|
|
|
match Router.target t ip with
|
2017-03-05 11:31:04 -05:00
|
|
|
| Some iface -> transmit_ipv4 packet iface
|
2019-03-17 17:32:17 -04:00
|
|
|
| None -> Lwt.return_unit
|
2015-12-30 11:07:16 -05:00
|
|
|
|
|
|
|
(* Packet classification *)
|
|
|
|
|
2019-04-17 06:03:17 -04:00
|
|
|
let parse_ips ips = List.map (fun (ip_str, id) -> (Ipaddr.of_string_exn ip_str, id)) ips
|
|
|
|
|
|
|
|
let clients = parse_ips Rules.clients
|
|
|
|
let externals = parse_ips Rules.externals
|
|
|
|
|
|
|
|
let resolve_client client =
|
|
|
|
`Client (try List.assoc (Ipaddr.V4 client#other_ip) clients with Not_found -> `Unknown)
|
|
|
|
|
|
|
|
let resolve_host = function
|
|
|
|
| `Client c -> resolve_client c
|
|
|
|
| `External ip -> `External (try List.assoc ip externals with Not_found -> `Unknown)
|
2019-05-16 14:18:31 -04:00
|
|
|
| (`Firewall | `NetVM) as x -> x
|
2019-04-17 06:03:17 -04:00
|
|
|
|
|
|
|
let classify ~src ~dst packet =
|
|
|
|
let `IPv4 (_ip, transport) = packet in
|
2015-12-30 11:07:16 -05:00
|
|
|
let proto =
|
2017-03-05 11:31:04 -05:00
|
|
|
match transport with
|
|
|
|
| `TCP ({Tcp.Tcp_packet.src_port; dst_port; _}, _) -> `TCP {sport = src_port; dport = dst_port}
|
|
|
|
| `UDP ({Udp_packet.src_port; dst_port; _}, _) -> `UDP {sport = src_port; dport = dst_port}
|
2017-03-07 05:02:54 -05:00
|
|
|
| `ICMP _ -> `ICMP
|
2017-03-05 11:31:04 -05:00
|
|
|
in
|
2015-12-30 11:07:16 -05:00
|
|
|
Some {
|
2017-03-05 11:31:04 -05:00
|
|
|
packet;
|
2019-04-17 06:03:17 -04:00
|
|
|
src;
|
|
|
|
dst;
|
2015-12-30 11:07:16 -05:00
|
|
|
proto;
|
|
|
|
}
|
|
|
|
|
2015-12-31 04:56:58 -05:00
|
|
|
let pp_ports fmt {sport; dport} =
|
|
|
|
Format.fprintf fmt "sport=%d dport=%d" sport dport
|
|
|
|
|
|
|
|
let pp_host fmt = function
|
2019-02-01 04:25:29 -05:00
|
|
|
| `Client c -> Ipaddr.V4.pp fmt (c#other_ip)
|
|
|
|
| `Unknown_client ip -> Format.fprintf fmt "unknown-client(%a)" Ipaddr.pp ip
|
2016-01-01 06:32:57 -05:00
|
|
|
| `NetVM -> Format.pp_print_string fmt "net-vm"
|
2019-02-01 04:25:29 -05:00
|
|
|
| `External ip -> Format.fprintf fmt "external(%a)" Ipaddr.pp ip
|
2019-05-16 14:18:31 -04:00
|
|
|
| `Firewall -> Format.pp_print_string fmt "firewall"
|
2015-12-31 04:56:58 -05:00
|
|
|
|
|
|
|
let pp_proto fmt = function
|
|
|
|
| `UDP ports -> Format.fprintf fmt "UDP(%a)" pp_ports ports
|
|
|
|
| `TCP ports -> Format.fprintf fmt "TCP(%a)" pp_ports ports
|
|
|
|
| `ICMP -> Format.pp_print_string fmt "ICMP"
|
|
|
|
| `Unknown -> Format.pp_print_string fmt "UnknownProtocol"
|
|
|
|
|
2019-04-17 06:03:17 -04:00
|
|
|
let pp_packet t fmt {src = _; dst = _; proto; packet} =
|
|
|
|
let `IPv4 (ip, _transport) = packet in
|
|
|
|
let src = Router.classify t (Ipaddr.V4 ip.Ipv4_packet.src) in
|
|
|
|
let dst = Router.classify t (Ipaddr.V4 ip.Ipv4_packet.dst) in
|
2015-12-31 04:56:58 -05:00
|
|
|
Format.fprintf fmt "[src=%a dst=%a proto=%a]"
|
|
|
|
pp_host src
|
|
|
|
pp_host dst
|
|
|
|
pp_proto proto
|
|
|
|
|
2017-04-08 08:12:38 -04:00
|
|
|
let pp_transport_headers f = function
|
|
|
|
| `ICMP (h, _) -> Icmpv4_packet.pp f h
|
|
|
|
| `TCP (h, _) -> Tcp.Tcp_packet.pp f h
|
|
|
|
| `UDP (h, _) -> Udp_packet.pp f h
|
|
|
|
|
|
|
|
let pp_header f = function
|
|
|
|
| `IPv4 (ip, transport) ->
|
|
|
|
Fmt.pf f "%a %a"
|
|
|
|
Ipv4_packet.pp ip
|
|
|
|
pp_transport_headers transport
|
|
|
|
|
2015-12-30 11:07:16 -05:00
|
|
|
(* NAT *)
|
|
|
|
|
2017-03-02 09:52:55 -05:00
|
|
|
let translate t packet =
|
|
|
|
My_nat.translate t.Router.nat packet
|
2015-12-30 11:07:16 -05:00
|
|
|
|
|
|
|
(* Add a NAT rule for the endpoints in this frame, via a random port on the firewall. *)
|
2017-03-02 09:52:55 -05:00
|
|
|
let add_nat_and_forward_ipv4 t packet =
|
2017-03-10 11:09:36 -05:00
|
|
|
let xl_host = t.Router.uplink#my_ip in
|
|
|
|
My_nat.add_nat_rule_and_translate t.Router.nat ~xl_host `NAT packet >>= function
|
2017-03-02 09:52:55 -05:00
|
|
|
| Ok packet -> forward_ipv4 t packet
|
|
|
|
| Error e ->
|
2017-04-08 08:12:38 -04:00
|
|
|
Log.warn (fun f -> f "Failed to add NAT rewrite rule: %s (%a)" e pp_header packet);
|
2017-03-02 09:52:55 -05:00
|
|
|
Lwt.return ()
|
2015-12-30 11:07:16 -05:00
|
|
|
|
2016-01-01 06:32:57 -05:00
|
|
|
(* Add a NAT rule to redirect this conversation to [host:port] instead of us. *)
|
2017-03-02 09:52:55 -05:00
|
|
|
let nat_to t ~host ~port packet =
|
2017-03-10 11:09:36 -05:00
|
|
|
match Router.resolve t host with
|
|
|
|
| Ipaddr.V6 _ -> Log.warn (fun f -> f "Cannot NAT with IPv6"); Lwt.return ()
|
|
|
|
| Ipaddr.V4 target ->
|
|
|
|
let xl_host = t.Router.uplink#my_ip in
|
|
|
|
My_nat.add_nat_rule_and_translate t.Router.nat ~xl_host (`Redirect (target, port)) packet >>= function
|
|
|
|
| Ok packet -> forward_ipv4 t packet
|
|
|
|
| Error e ->
|
2017-04-08 08:12:38 -04:00
|
|
|
Log.warn (fun f -> f "Failed to add NAT redirect rule: %s (%a)" e pp_header packet);
|
2017-03-10 11:09:36 -05:00
|
|
|
Lwt.return ()
|
2015-12-30 11:07:16 -05:00
|
|
|
|
|
|
|
(* Handle incoming packets *)
|
|
|
|
|
2019-04-17 06:03:17 -04:00
|
|
|
let apply_rules t rules ~dst info =
|
2017-03-02 09:52:55 -05:00
|
|
|
let packet = info.packet in
|
2019-04-17 06:03:17 -04:00
|
|
|
match rules info, dst with
|
2017-03-05 11:31:04 -05:00
|
|
|
| `Accept, `Client client_link -> transmit_ipv4 packet client_link
|
|
|
|
| `Accept, (`External _ | `NetVM) -> transmit_ipv4 packet t.Router.uplink
|
2019-05-16 14:18:31 -04:00
|
|
|
| `Accept, `Firewall ->
|
2019-04-17 06:03:17 -04:00
|
|
|
Log.warn (fun f -> f "Bad rule: firewall can't accept packets %a" (pp_packet t) info);
|
2016-01-01 07:54:44 -05:00
|
|
|
return ()
|
2017-03-02 09:52:55 -05:00
|
|
|
| `NAT, _ -> add_nat_and_forward_ipv4 t packet
|
|
|
|
| `NAT_to (host, port), _ -> nat_to t packet ~host ~port
|
2016-01-01 07:54:44 -05:00
|
|
|
| `Drop reason, _ ->
|
2019-04-17 06:03:17 -04:00
|
|
|
Log.info (fun f -> f "Dropped packet (%s) %a" reason (pp_packet t) info);
|
2016-01-01 07:54:44 -05:00
|
|
|
return ()
|
|
|
|
|
2016-01-02 10:50:05 -05:00
|
|
|
let handle_low_memory t =
|
2015-12-30 11:07:16 -05:00
|
|
|
match Memory_pressure.status () with
|
|
|
|
| `Memory_critical -> (* TODO: should happen before copying and async *)
|
2016-01-08 06:31:27 -05:00
|
|
|
Log.warn (fun f -> f "Memory low - dropping packet and resetting NAT table");
|
2017-03-02 09:52:55 -05:00
|
|
|
My_nat.reset t.Router.nat >|= fun () ->
|
2016-01-02 10:50:05 -05:00
|
|
|
`Memory_critical
|
2017-03-02 09:52:55 -05:00
|
|
|
| `Ok -> Lwt.return `Ok
|
2016-01-02 10:50:05 -05:00
|
|
|
|
2019-04-17 06:03:17 -04:00
|
|
|
let ipv4_from_client t ~src packet =
|
2017-03-02 09:52:55 -05:00
|
|
|
handle_low_memory t >>= function
|
2016-01-02 10:50:05 -05:00
|
|
|
| `Memory_critical -> return ()
|
2015-12-30 11:07:16 -05:00
|
|
|
| `Ok ->
|
|
|
|
(* Check for existing NAT entry for this packet *)
|
2017-03-05 11:31:04 -05:00
|
|
|
translate t packet >>= function
|
2015-12-30 11:07:16 -05:00
|
|
|
| Some frame -> forward_ipv4 t frame (* Some existing connection or redirect *)
|
|
|
|
| None ->
|
|
|
|
(* No existing NAT entry. Check the firewall rules. *)
|
2019-04-17 06:03:17 -04:00
|
|
|
let `IPv4 (ip, _transport) = packet in
|
|
|
|
let dst = Router.classify t (Ipaddr.V4 ip.Ipv4_packet.dst) in
|
|
|
|
match classify ~src:(resolve_client src) ~dst:(resolve_host dst) packet with
|
2015-12-30 11:07:16 -05:00
|
|
|
| None -> return ()
|
2019-04-17 06:03:17 -04:00
|
|
|
| Some info -> apply_rules t Rules.from_client ~dst info
|
2015-12-30 11:07:16 -05:00
|
|
|
|
2017-03-05 11:31:04 -05:00
|
|
|
let ipv4_from_netvm t packet =
|
2017-03-02 09:52:55 -05:00
|
|
|
handle_low_memory t >>= function
|
2016-01-02 10:50:05 -05:00
|
|
|
| `Memory_critical -> return ()
|
2015-12-30 11:07:16 -05:00
|
|
|
| `Ok ->
|
2019-04-17 06:03:17 -04:00
|
|
|
let `IPv4 (ip, _transport) = packet in
|
|
|
|
let src = Router.classify t (Ipaddr.V4 ip.Ipv4_packet.src) in
|
|
|
|
let dst = Router.classify t (Ipaddr.V4 ip.Ipv4_packet.dst) in
|
|
|
|
match classify ~src ~dst:(resolve_host dst) packet with
|
2015-12-30 11:07:16 -05:00
|
|
|
| None -> return ()
|
|
|
|
| Some info ->
|
2019-04-17 06:03:17 -04:00
|
|
|
match src with
|
2019-05-16 14:18:31 -04:00
|
|
|
| `Client _ | `Firewall ->
|
2019-04-17 06:03:17 -04:00
|
|
|
Log.warn (fun f -> f "Frame from NetVM has internal source IP address! %a" (pp_packet t) info);
|
2015-12-30 11:07:16 -05:00
|
|
|
return ()
|
2019-04-17 06:03:17 -04:00
|
|
|
| `External _ | `NetVM as src ->
|
2017-03-05 11:31:04 -05:00
|
|
|
translate t packet >>= function
|
2015-12-30 11:07:16 -05:00
|
|
|
| Some frame -> forward_ipv4 t frame
|
|
|
|
| None ->
|
2019-04-17 06:03:17 -04:00
|
|
|
apply_rules t Rules.from_netvm ~dst { info with src }
|