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
|
|
|
|
|
|
|
|
type ports = {
|
|
|
|
sport : port; (* Source port *)
|
|
|
|
dport : port; (* Destination *)
|
|
|
|
}
|
|
|
|
|
|
|
|
type host =
|
2016-09-25 09:38:17 -04:00
|
|
|
[ `Client of client_link | `Client_gateway | `Firewall_uplink | `NetVM | `External of Ipaddr.t ]
|
2015-12-30 11:07:16 -05:00
|
|
|
|
2019-04-17 06:03:17 -04:00
|
|
|
type ('src, 'dst) info = {
|
2017-03-05 11:31:04 -05:00
|
|
|
packet : Nat_packet.t;
|
2019-04-17 06:03:17 -04:00
|
|
|
src : 'src;
|
|
|
|
dst : 'dst;
|
2015-12-30 11:07:16 -05:00
|
|
|
proto : [ `UDP of ports | `TCP of ports | `ICMP | `Unknown ];
|
|
|
|
}
|
2019-04-11 07:25:19 -04:00
|
|
|
|
|
|
|
(* The first message in a TCP connection has SYN set and ACK clear. *)
|
|
|
|
let is_tcp_start = function
|
|
|
|
| `IPv4 (_ip, `TCP (hdr, _body)) -> Tcp.Tcp_packet.(hdr.syn && not hdr.ack)
|
|
|
|
| _ -> false
|
2019-04-17 05:26:32 -04:00
|
|
|
|
|
|
|
(* The possible actions we can take for a packet: *)
|
|
|
|
type action = [
|
|
|
|
| `Accept (* Send the packet to its destination. *)
|
|
|
|
| `NAT (* Rewrite the packet's source field so packet appears to
|
|
|
|
have come from the firewall, via an unused port.
|
|
|
|
Also, add NAT rules so related packets will be translated accordingly. *)
|
|
|
|
| `NAT_to of host * port (* As for [`NAT], but also rewrite the packet's
|
|
|
|
destination fields so it will be sent to [host:port]. *)
|
|
|
|
| `Drop of string (* Drop the packet and log the given reason. *)
|
|
|
|
]
|