Skip to content

Commit

Permalink
Unlock the usage of randomconv/mirage-crypto-rng about our Cstruct.t …
Browse files Browse the repository at this point in the history
…-> string on mirage-tcpip
  • Loading branch information
dinosaure committed Jun 27, 2024
1 parent e422baa commit 53718a6
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/ipv4/static_ipv4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

open Lwt.Infix

let ( % ) f g = fun x -> f (g x)
let src = Logs.Src.create "ipv4" ~doc:"Mirage IPv4"
module Log = (val Logs.src_log src : Logs.LOG)

Expand Down Expand Up @@ -77,7 +78,7 @@ module Make (R: Mirage_random.S) (C: Mirage_clock.MCLOCK) (Ethernet: Ethernet.S)
in
let hdr =
let src = match src with None -> Ipaddr.V4.Prefix.address t.cidr | Some x -> x in
let id = if multiple then Randomconv.int16 R.generate else 0 in
let id = if multiple then Randomconv.int16 (Cstruct.to_string % R.generate) else 0 in
Ipv4_packet.{
options = Cstruct.empty ;
src ; dst ; ttl ; off ; id ;
Expand Down
3 changes: 2 additions & 1 deletion src/ipv6/ndpv6.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ References:
http://tools.ietf.org/html/rfc3810
*)

let ( % ) f g = fun x -> f (g x)
let src = Logs.Src.create "ndpc6" ~doc:"Mirage IPv6 discovery"
module Log = (val Logs.src_log src : Logs.LOG)

Expand Down Expand Up @@ -120,7 +121,7 @@ let multicast_mac =
let compute_reachable_time r reachable_time =
let factor =
Defaults.min_random_factor +.
Randomconv.float ~bound:Defaults.(max_random_factor -. min_random_factor) r
Randomconv.float ~bound:Defaults.(max_random_factor -. min_random_factor) (Cstruct.to_string % r)
in
Int64.of_float (factor *. Int64.to_float reachable_time)

Expand Down
8 changes: 5 additions & 3 deletions src/tcp/flow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

open Lwt.Infix

let ( % ) f g = fun x -> f (g x)

let src = Logs.Src.create "tcp.pcb" ~doc:"Mirage TCP PCB module"
module Log = (val Logs.src_log src : Logs.LOG)

Expand Down Expand Up @@ -510,7 +512,7 @@ struct
log_with_stats "process-syn" t;
match Hashtbl.find_opt t.listeners (WIRE.src_port id) with
| Some (keepalive, process) ->
let tx_isn = Sequence.of_int32 (Randomconv.int32 Random.generate) in
let tx_isn = Sequence.of_int32 (Randomconv.int32 (Cstruct.to_string % Random.generate)) in
(* TODO: make this configurable per listener *)
let rx_wnd = 65535 in
let rx_wnd_scaleoffer = wscale_default in
Expand Down Expand Up @@ -703,7 +705,7 @@ struct

let connect ?keepalive t ~dst ~dst_port =
let id = getid t dst dst_port in
let tx_isn = Sequence.of_int32 (Randomconv.int32 Random.generate) in
let tx_isn = Sequence.of_int32 (Randomconv.int32 (Cstruct.to_string % Random.generate)) in
(* TODO: This is hardcoded for now - make it configurable *)
let rx_wnd_scaleoffer = wscale_default in
let options =
Expand Down Expand Up @@ -751,7 +753,7 @@ struct
(* Construct the main TCP thread *)
let connect ip =
let localport =
1024 + (Randomconv.int ~bound:(0xFFFF - 1024) Random.generate)
1024 + (Randomconv.int ~bound:(0xFFFF - 1024) (Cstruct.to_string % Random.generate))
in
let listens = Hashtbl.create 1 in
let connects = Hashtbl.create 1 in
Expand Down
2 changes: 1 addition & 1 deletion src/udp/udp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module Make (Ip : Tcpip.Ip.S) (Random : Mirage_random.S) = struct

let writev ?src ?src_port ?ttl ~dst ~dst_port t bufs =
let src_port = match src_port with
| None -> Randomconv.int ~bound:65535 (fun x -> Random.generate x)
| None -> Randomconv.int ~bound:65535 (fun x -> Cstruct.to_string (Random.generate x))
| Some p -> p
in
let fill_hdr buf =
Expand Down
6 changes: 5 additions & 1 deletion tcpip.opam
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ depends: [
"lwt-dllist"
"logs" {>= "0.6.0"}
"duration"
"randomconv" {< "0.2.0"}
"randomconv" {>= "0.2.0"}
"ethernet" {>= "3.0.0"}
"arp" {>= "3.0.0"}
"mirage-flow" {>= "4.0.0"}
Expand All @@ -66,3 +66,7 @@ system](https://mirage.io). It provides implementations for the following module
* UDP
* TCP
"""

pin-depends: [
[ "mirage-crypto-rng.0.11.0" "git+https://github.com/mirage/mirage-crypto.git#98f01b14f5ebf98ba0e7e9c2ba97ec518f90fddc" ]
]
7 changes: 6 additions & 1 deletion test/low_level.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ module Time = Vnetif_common.Time
module V = Vnetif.Make(Vnetif_backends.Basic)
module E = Ethernet.Make(V)
module A = Arp.Make(E)(Time)
module I = Static_ipv4.Make(Mirage_crypto_rng)(Vnetif_common.Clock)(E)(A)
module Rng = struct
include Mirage_crypto_rng

let generate ?g n = Cstruct.of_string (generate ?g n)
end
module I = Static_ipv4.Make(Rng)(Vnetif_common.Clock)(E)(A)
module Wire = Tcp.Wire
module WIRE = Wire.Make(I)
module Tcp_wire = Tcp.Tcp_wire
Expand Down
10 changes: 8 additions & 2 deletions test/test_deadlock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ module Server_log = (val Logs.src_log server_log : Logs.LOG)
let client_log = Logs.Src.create "test_deadlock_client" ~doc:"tcp deadlock tests: client"
module Client_log = (val Logs.src_log client_log : Logs.LOG)

module Rng = struct
include Mirage_crypto_rng

let generate ?g n = Cstruct.of_string (generate ?g n)
end

module TCPIP =
struct
module RANDOM = Mirage_crypto_rng
module RANDOM = Rng

module TIME =
struct
Expand Down Expand Up @@ -79,7 +85,7 @@ let test_digest netif1 netif2 =
TCPIP.make `Server netif2 >>= fun server_stack ->

let send_data () =
let data = Mirage_crypto_rng.generate 100_000_000 |> Cstruct.to_string in
let data = Mirage_crypto_rng.generate 100_000_000 in
let t0 = Unix.gettimeofday () in
TCPIP.TCP.create_connection
TCPIP.(tcp @@ tcpip server_stack) (Ipaddr.V4 TCPIP.client_ip, port) >>= function
Expand Down
9 changes: 7 additions & 2 deletions test/test_icmpv4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module B = Basic_backend.Make
module V = Vnetif.Make(B)
module E = Ethernet.Make(V)
module Static_arp = Static_arp.Make(E)(Time)
module Rng = struct
include Mirage_crypto_rng

let generate ?g n = Cstruct.of_string (generate ?g n)
end

open Lwt.Infix

Expand All @@ -18,10 +23,10 @@ type decomposed = {
ethernet_header : Ethernet.Packet.t;
}

module Ip = Static_ipv4.Make(Mirage_crypto_rng)(Mclock)(E)(Static_arp)
module Ip = Static_ipv4.Make(Rng)(Mclock)(E)(Static_arp)
module Icmp = Icmpv4.Make(Ip)

module Udp = Udp.Make(Ip)(Mirage_crypto_rng)
module Udp = Udp.Make(Ip)(Rng)

type stack = {
backend : B.t;
Expand Down
9 changes: 7 additions & 2 deletions test/test_ipv6.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ module Time = Vnetif_common.Time
module B = Vnetif_backends.Basic
module V = Vnetif.Make(B)
module E = Ethernet.Make(V)
module Rng = struct
include Mirage_crypto_rng

module Ipv6 = Ipv6.Make(V)(E)(Mirage_crypto_rng)(Time)(Mclock)
module Udp = Udp.Make(Ipv6)(Mirage_crypto_rng)
let generate ?g n = Cstruct.of_string (generate ?g n)
end

module Ipv6 = Ipv6.Make(V)(E)(Rng)(Time)(Mclock)
module Udp = Udp.Make(Ipv6)(Rng)
open Lwt.Infix

let ip =
Expand Down
9 changes: 7 additions & 2 deletions test/test_udp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ module B = Basic_backend.Make
module V = Vnetif.Make(B)
module E = Ethernet.Make(V)
module Static_arp = Static_arp.Make(E)(Time)
module Ip = Static_ipv4.Make(Mirage_crypto_rng)(Mclock)(E)(Static_arp)
module Udp = Udp.Make(Ip)(Mirage_crypto_rng)
module Rng = struct
include Mirage_crypto_rng

let generate ?g n = Cstruct.of_string (generate ?g n)
end
module Ip = Static_ipv4.Make(Rng)(Mclock)(E)(Static_arp)
module Udp = Udp.Make(Ip)(Rng)

type stack = {
backend : B.t;
Expand Down
16 changes: 11 additions & 5 deletions test/vnetif_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ module Time = struct
let sleep_ns ns = sleep (Duration.to_f ns)
end
module Clock = Mclock
module Rng = struct
include Mirage_crypto_rng

let generate ?g n =
Cstruct.of_string (generate ?g n)
end

module type VNETIF_STACK =
sig
Expand Down Expand Up @@ -62,15 +68,15 @@ end
module E = Ethernet.Make(V)

module A = Arp.Make(E)(Time)
module Ip4 = Static_ipv4.Make(Mirage_crypto_rng)(Clock)(E)(A)
module Ip4 = Static_ipv4.Make(Rng)(Clock)(E)(A)
module Icmp4 = Icmpv4.Make(Ip4)
module Ip6 = Ipv6.Make(V)(E)(Mirage_crypto_rng)(Time)(Clock)
module Ip6 = Ipv6.Make(V)(E)(Rng)(Time)(Clock)
module Ip46 = Tcpip_stack_direct.IPV4V6(Ip4)(Ip6)
module U = Udp.Make(Ip46)(Mirage_crypto_rng)
module T = Tcp.Flow.Make(Ip46)(Time)(Clock)(Mirage_crypto_rng)
module U = Udp.Make(Ip46)(Rng)
module T = Tcp.Flow.Make(Ip46)(Time)(Clock)(Rng)

module Stack =
Tcpip_stack_direct.MakeV4V6(Time)(Mirage_crypto_rng)(V)(E)(A)(Ip46)(Icmp4)(U)(T)
Tcpip_stack_direct.MakeV4V6(Time)(Rng)(V)(E)(A)(Ip46)(Icmp4)(U)(T)

let create_backend () =
B.create ()
Expand Down

0 comments on commit 53718a6

Please sign in to comment.