2015-12-30 11:07:16 -05:00
|
|
|
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
|
|
|
|
See the README file for details. *)
|
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
(** This module applies firewall rules from QubesDB. *)
|
2015-12-30 11:07:16 -05:00
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
open Packet
|
|
|
|
open Lwt.Infix
|
|
|
|
module Q = Pf_qubes.Parse_qubes
|
2015-12-30 11:07:16 -05:00
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
let src = Logs.Src.create "rules" ~doc:"Firewall rules"
|
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
2019-04-11 07:25:19 -04:00
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
(* the upstream NetVM will redirect TCP and UDP port 53 traffic with
|
|
|
|
these destination IPs to its upstream nameserver. *)
|
|
|
|
let default_dns_servers = [
|
|
|
|
Ipaddr.V4.of_string_exn "10.139.1.1";
|
|
|
|
Ipaddr.V4.of_string_exn "10.139.1.2";
|
2019-04-11 07:25:19 -04:00
|
|
|
]
|
2020-04-29 09:58:01 -04:00
|
|
|
let dns_port = 53
|
|
|
|
|
|
|
|
module Classifier = struct
|
|
|
|
|
|
|
|
let matches_port dstports (port : int) = match dstports with
|
|
|
|
| None -> true
|
|
|
|
| Some (Q.Range_inclusive (min, max)) -> min <= port && port <= max
|
|
|
|
|
|
|
|
let matches_proto rule packet = match rule.Q.proto, rule.Q.specialtarget with
|
|
|
|
| None, None -> true
|
|
|
|
| None, Some `dns when List.mem packet.ipv4_header.Ipv4_packet.dst default_dns_servers -> begin
|
|
|
|
(* specialtarget=dns applies only to the specialtarget destination IPs, and
|
|
|
|
specialtarget=dns is also implicitly tcp/udp port 53 *)
|
|
|
|
match packet.transport_header with
|
|
|
|
| `TCP header -> header.Tcp.Tcp_packet.dst_port = dns_port
|
|
|
|
| `UDP header -> header.Udp_packet.dst_port = dns_port
|
|
|
|
| _ -> false
|
|
|
|
end
|
|
|
|
(* DNS rules can only match traffic headed to the specialtarget hosts, so any other destination
|
|
|
|
isn't a match for DNS rules *)
|
|
|
|
| None, Some `dns -> false
|
|
|
|
| Some rule_proto, _ -> match rule_proto, packet.transport_header with
|
|
|
|
| `tcp, `TCP header -> matches_port rule.Q.dstports header.Tcp.Tcp_packet.dst_port
|
|
|
|
| `udp, `UDP header -> matches_port rule.Q.dstports header.Udp_packet.dst_port
|
|
|
|
| `icmp, `ICMP header ->
|
|
|
|
begin
|
|
|
|
match rule.Q.icmp_type with
|
|
|
|
| None -> true
|
|
|
|
| Some rule_icmp_type ->
|
|
|
|
0 = compare rule_icmp_type @@ Icmpv4_wire.ty_to_int header.Icmpv4_packet.ty
|
|
|
|
end
|
|
|
|
| _, _ -> false
|
|
|
|
|
2020-04-29 10:06:48 -04:00
|
|
|
let matches_dest dns_client rule packet =
|
2020-04-29 09:58:01 -04:00
|
|
|
let ip = packet.ipv4_header.Ipv4_packet.dst in
|
|
|
|
match rule.Q.dst with
|
|
|
|
| `any -> Lwt.return @@ `Match rule
|
|
|
|
| `hosts subnet ->
|
|
|
|
Lwt.return @@ if (Ipaddr.Prefix.mem Ipaddr.(V4 ip) subnet) then `Match rule else `No_match
|
|
|
|
| `dnsname name ->
|
2020-04-29 10:06:48 -04:00
|
|
|
Log.debug (fun f -> f "Resolving %a" Domain_name.pp name);
|
|
|
|
dns_client name >|= function
|
|
|
|
| Ok (_ttl, found_ips) ->
|
2021-10-28 07:39:32 -04:00
|
|
|
if Ipaddr.V4.Set.mem ip found_ips
|
2020-04-29 10:06:48 -04:00
|
|
|
then `Match rule
|
|
|
|
else `No_match
|
|
|
|
| Error (`Msg m) ->
|
|
|
|
Log.warn (fun f -> f "Ignoring rule %a, could not resolve" Q.pp_rule rule);
|
|
|
|
Log.debug (fun f -> f "%s" m);
|
|
|
|
`No_match
|
|
|
|
| Error _ -> assert false (* TODO: fix type of dns_client so that this case can go *)
|
2020-04-29 09:58:01 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2020-04-29 10:06:48 -04:00
|
|
|
let find_first_match dns_client packet acc rule =
|
2020-04-29 09:58:01 -04:00
|
|
|
match acc with
|
|
|
|
| `No_match ->
|
|
|
|
if Classifier.matches_proto rule packet
|
2020-04-29 10:06:48 -04:00
|
|
|
then Classifier.matches_dest dns_client rule packet
|
2020-04-29 09:58:01 -04:00
|
|
|
else Lwt.return `No_match
|
|
|
|
| q -> Lwt.return q
|
|
|
|
|
|
|
|
(* Does the packet match our rules? *)
|
2020-04-29 10:06:48 -04:00
|
|
|
let classify_client_packet dns_client (packet : ([`Client of Fw_utils.client_link], _) Packet.t) =
|
2020-04-29 09:58:01 -04:00
|
|
|
let (`Client client_link) = packet.src in
|
|
|
|
let rules = client_link#get_rules in
|
2020-04-29 10:06:48 -04:00
|
|
|
Lwt_list.fold_left_s (find_first_match dns_client packet) `No_match rules >|= function
|
2020-04-29 09:58:01 -04:00
|
|
|
| `No_match -> `Drop "No matching rule; assuming default drop"
|
|
|
|
| `Match {Q.action = Q.Accept; _} -> `Accept
|
|
|
|
| `Match ({Q.action = Q.Drop; _} as rule) ->
|
|
|
|
`Drop (Format.asprintf "rule number %a explicitly drops this packet" Q.pp_rule rule)
|
|
|
|
|
2020-04-29 10:06:48 -04:00
|
|
|
let translate_accepted_packets dns_client packet =
|
|
|
|
classify_client_packet dns_client packet >|= function
|
2020-04-29 09:58:01 -04:00
|
|
|
| `Accept -> `NAT
|
|
|
|
| `Drop s -> `Drop s
|
|
|
|
|
|
|
|
(** Packets from the private interface that don't match any NAT table entry are being checked against the fw rules here *)
|
2020-04-29 10:06:48 -04:00
|
|
|
let from_client dns_client (packet : ([`Client of Fw_utils.client_link], _) Packet.t) : Packet.action Lwt.t =
|
2020-04-29 09:58:01 -04:00
|
|
|
match packet with
|
2020-04-29 10:06:48 -04:00
|
|
|
| { dst = `External _ ; _ } | { dst = `NetVM; _ } -> translate_accepted_packets dns_client packet
|
2020-04-29 09:58:01 -04:00
|
|
|
| { dst = `Firewall ; _ } -> Lwt.return @@ `Drop "packet addressed to firewall itself"
|
2020-04-29 10:06:48 -04:00
|
|
|
| { dst = `Client _ ; _ } -> classify_client_packet dns_client packet
|
2020-04-29 09:58:01 -04:00
|
|
|
| _ -> Lwt.return @@ `Drop "could not classify packet"
|
2019-04-11 07:25:19 -04:00
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
(** Packets from the outside world that don't match any NAT table entry are being dropped by default *)
|
|
|
|
let from_netvm (_packet : ([`NetVM | `External of _], _) Packet.t) : Packet.action Lwt.t =
|
|
|
|
Lwt.return @@ `Drop "drop by default"
|