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
|
|
|
|
|
|
|
type port = int
|
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
type host =
|
2019-05-16 14:18:31 -04:00
|
|
|
[ `Client of client_link | `Firewall | `NetVM | `External of Ipaddr.t ]
|
2015-12-30 11:07:16 -05:00
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
type transport_header = [`TCP of Tcp.Tcp_packet.t
|
|
|
|
|`UDP of Udp_packet.t
|
|
|
|
|`ICMP of Icmpv4_packet.t]
|
|
|
|
|
|
|
|
type ('src, 'dst) t = {
|
|
|
|
ipv4_header : Ipv4_packet.t;
|
|
|
|
transport_header : transport_header;
|
|
|
|
transport_payload : Cstruct.t;
|
2019-04-17 06:03:17 -04:00
|
|
|
src : 'src;
|
|
|
|
dst : 'dst;
|
2015-12-30 11:07:16 -05:00
|
|
|
}
|
2020-04-29 09:58:01 -04:00
|
|
|
let pp_transport_header 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_host fmt = function
|
|
|
|
| `Client c -> Ipaddr.V4.pp fmt (c#other_ip)
|
|
|
|
| `Unknown_client ip -> Format.fprintf fmt "unknown-client(%a)" Ipaddr.pp ip
|
|
|
|
| `NetVM -> Format.pp_print_string fmt "net-vm"
|
|
|
|
| `External ip -> Format.fprintf fmt "external(%a)" Ipaddr.pp ip
|
|
|
|
| `Firewall -> Format.pp_print_string fmt "firewall(client-gw)"
|
|
|
|
|
|
|
|
let to_mirage_nat_packet t : Nat_packet.t =
|
|
|
|
match t.transport_header with
|
|
|
|
| `TCP h -> `IPv4 (t.ipv4_header, (`TCP (h, t.transport_payload)))
|
|
|
|
| `UDP h -> `IPv4 (t.ipv4_header, (`UDP (h, t.transport_payload)))
|
|
|
|
| `ICMP h -> `IPv4 (t.ipv4_header, (`ICMP (h, t.transport_payload)))
|
2019-04-11 07:25:19 -04:00
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
let of_mirage_nat_packet ~src ~dst packet : ('a, 'b) t option =
|
|
|
|
let `IPv4 (ipv4_header, ipv4_payload) = packet in
|
|
|
|
let transport_header, transport_payload = match ipv4_payload with
|
|
|
|
| `TCP (h, p) -> `TCP h, p
|
|
|
|
| `UDP (h, p) -> `UDP h, p
|
|
|
|
| `ICMP (h, p) -> `ICMP h, p
|
|
|
|
in
|
|
|
|
Some {
|
|
|
|
ipv4_header;
|
|
|
|
transport_header;
|
|
|
|
transport_payload;
|
|
|
|
src;
|
|
|
|
dst;
|
|
|
|
}
|
2019-04-17 05:26:32 -04:00
|
|
|
|
2020-04-29 09:58:01 -04:00
|
|
|
(* possible actions to take for a packet: *)
|
2019-04-17 05:26:32 -04:00
|
|
|
type action = [
|
2020-04-29 09:58:01 -04:00
|
|
|
| `Accept (* Send to destination, unmodified. *)
|
|
|
|
| `NAT (* Rewrite source field to the firewall's IP, with a fresh source port.
|
|
|
|
Also, add translation rules for future traffic in both directions,
|
|
|
|
between these hosts on these ports, and corresponding ICMP error traffic. *)
|
2019-04-17 05:26:32 -04:00
|
|
|
| `NAT_to of host * port (* As for [`NAT], but also rewrite the packet's
|
|
|
|
destination fields so it will be sent to [host:port]. *)
|
2020-04-29 09:58:01 -04:00
|
|
|
| `Drop of string (* Drop packet for this reason. *)
|
2019-04-17 05:26:32 -04:00
|
|
|
]
|