From 1bc27cd57dc0d79c70d2312bb9396c88df38aef0 Mon Sep 17 00:00:00 2001 From: Calascibetta Romain Date: Thu, 27 Jun 2024 06:36:37 +0200 Subject: [PATCH] Unlock the usage of randomconv/mirage-crypto-rng about our Cstruct.t -> string on mirage-tcpip --- src/ipv4/static_ipv4.ml | 3 ++- src/ipv6/ndpv6.ml | 3 ++- src/tcp/flow.ml | 8 +++++--- src/udp/udp.ml | 2 +- tcpip.opam | 7 ++++++- test/low_level.ml | 7 ++++++- test/test_deadlock.ml | 10 ++++++++-- test/test_icmpv4.ml | 9 +++++++-- test/test_ipv6.ml | 9 +++++++-- test/test_udp.ml | 9 +++++++-- test/vnetif_common.ml | 16 +++++++++++----- 11 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/ipv4/static_ipv4.ml b/src/ipv4/static_ipv4.ml index 1b9065d7..c9604ab9 100644 --- a/src/ipv4/static_ipv4.ml +++ b/src/ipv4/static_ipv4.ml @@ -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) @@ -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 ; diff --git a/src/ipv6/ndpv6.ml b/src/ipv6/ndpv6.ml index effc6688..740799f4 100644 --- a/src/ipv6/ndpv6.ml +++ b/src/ipv6/ndpv6.ml @@ -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) @@ -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) diff --git a/src/tcp/flow.ml b/src/tcp/flow.ml index a1ccf9d5..59bfb125 100644 --- a/src/tcp/flow.ml +++ b/src/tcp/flow.ml @@ -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) @@ -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 @@ -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 = @@ -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 diff --git a/src/udp/udp.ml b/src/udp/udp.ml index 347b681f..fd2278ad 100644 --- a/src/udp/udp.ml +++ b/src/udp/udp.ml @@ -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 = diff --git a/tcpip.opam b/tcpip.opam index 2a504876..30195b7e 100644 --- a/tcpip.opam +++ b/tcpip.opam @@ -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"} @@ -66,3 +66,8 @@ 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" ] + [ "mirage-crypto.0.11.0" "git+https://github.com/mirage/mirage-crypto.git#98f01b14f5ebf98ba0e7e9c2ba97ec518f90fddc" ] +] diff --git a/test/low_level.ml b/test/low_level.ml index 5c4f59c6..6489e88f 100644 --- a/test/low_level.ml +++ b/test/low_level.ml @@ -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 diff --git a/test/test_deadlock.ml b/test/test_deadlock.ml index d7efd616..6de61d8e 100644 --- a/test/test_deadlock.ml +++ b/test/test_deadlock.ml @@ -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 @@ -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 diff --git a/test/test_icmpv4.ml b/test/test_icmpv4.ml index 2b26a1b6..b830e40b 100644 --- a/test/test_icmpv4.ml +++ b/test/test_icmpv4.ml @@ -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 @@ -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; diff --git a/test/test_ipv6.ml b/test/test_ipv6.ml index fcea9fa9..c9fd15f0 100644 --- a/test/test_ipv6.ml +++ b/test/test_ipv6.ml @@ -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 = diff --git a/test/test_udp.ml b/test/test_udp.ml index d049f567..4cea792f 100644 --- a/test/test_udp.ml +++ b/test/test_udp.ml @@ -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; diff --git a/test/vnetif_common.ml b/test/vnetif_common.ml index 838d46ce..8ec66732 100644 --- a/test/vnetif_common.ml +++ b/test/vnetif_common.ml @@ -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 @@ -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 ()