Source file ICMPv4.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
let src = Logs.Src.create "icmpv4"

module Log = (val Logs.src_log src : Logs.LOG)

module Packet = struct
  type 'a t = { code: int; kind: 'a kind; checksum: int; shdr: 'a }

  and 'a kind =
    | Echo_reply : id_and_seq kind
    | Destination_unreachable : next_hop_mtu kind
    | Source_quench : unused kind
    | Redirect : Ipaddr.V4.t kind
    | Echo_request : id_and_seq kind
    | Time_exceeded : unused kind
    | Parameter_problem : pointer kind
    | Timestamp_request : id_and_seq kind
    | Timestamp_reply : id_and_seq kind
    | Information_request : id_and_seq kind
    | Information_reply : id_and_seq kind

  and id_and_seq = int * int
  and next_hop_mtu = Hop of int [@@unboxed]
  and pointer = Pointer of int [@@unboxed]
  and unused = Unused
  and pack = Kind : 'a kind -> pack
  and packet = Packet : 'a t -> packet

  open Bin

  let kind =
    let f = function
      | 0 -> Kind Echo_reply
      | 3 -> Kind Destination_unreachable
      | 4 -> Kind Source_quench
      | 5 -> Kind Redirect
      | 8 -> Kind Echo_request
      | 11 -> Kind Time_exceeded
      | 12 -> Kind Parameter_problem
      | 13 -> Kind Timestamp_request
      | 14 -> Kind Timestamp_reply
      | 15 -> Kind Information_request
      | 16 -> Kind Information_reply
      | _ -> invalid_arg "Invalid ICMPv4 message"
    in
    let g = function
      | Kind Echo_reply -> 0
      | Kind Destination_unreachable -> 3
      | Kind Source_quench -> 4
      | Kind Redirect -> 5
      | Kind Echo_request -> 8
      | Kind Time_exceeded -> 11
      | Kind Parameter_problem -> 12
      | Kind Timestamp_request -> 13
      | Kind Timestamp_reply -> 14
      | Kind Information_request -> 15
      | Kind Information_reply -> 16
    in
    map uint8 f g

  let unused = const Unused
  let ipaddr = map beint32 Ipaddr.V4.of_int32 Ipaddr.V4.to_int32

  let id_and_seq =
    record (fun id seq -> (id, seq))
    |+ field beuint16 fst
    |+ field beuint16 snd
    |> sealr

  let next_hop_mtu =
    let f arr = Hop arr.(1) in
    let g (Hop mtu) = [| 0; mtu |] in
    map (seq ~len:2 beuint16) f g

  let pointer =
    let f byte = Pointer byte in
    let g (Pointer byte) = byte in
    map uint8 f g

  let shdr : type a. a kind -> a Bin.t = function
    | Echo_request -> id_and_seq
    | Echo_reply -> id_and_seq
    | Timestamp_request -> id_and_seq
    | Timestamp_reply -> id_and_seq
    | Information_request -> id_and_seq
    | Information_reply -> id_and_seq
    | Destination_unreachable -> next_hop_mtu
    | Time_exceeded -> unused
    | Source_quench -> unused
    | Redirect -> ipaddr
    | Parameter_problem -> pointer

  let t ~kind:knd =
    let fn _knd code checksum shdr = { kind= knd; code; checksum; shdr } in
    record fn
    |+ field kind (Fun.const (Kind knd))
    |+ field uint8 (fun t -> t.code)
    |+ field beuint16 (fun t -> t.checksum)
    |+ field (shdr knd) (fun t -> t.shdr)
    |> sealr

  let decode ?(off = 0) str =
    let pos = off in
    let (Kind kind) = decode kind str (ref off) in
    let off = ref off in
    let pkt = decode (t ~kind) str off in
    (* TODO(dinosaure): can we avoid this copy? *)
    let buf = Bytes.of_string str in
    let len = String.length str in
    Bytes.set_uint16_be buf (pos + 2) 0;
    let chk =
      Utcp.Checksum.digest_string ~off:pos ~len (Bytes.unsafe_to_string buf)
    in
    Log.debug (fun m -> m "checksum: %04x, has: %04x" pkt.checksum chk);
    if pkt.checksum != chk then invalid_arg "Invalid ICMPv4 checksum";
    let payload = String.sub str !off (String.length str - !off) in
    (Packet pkt, payload)

  let decode ?off bstr =
    try Ok (decode ?off bstr)
    with exn ->
      Log.err (fun m ->
          m "Got an exception while decoding ICMPv4 packet: %s"
            (Printexc.to_string exn));
      Error `Invalid_ICMPv4_packet

  let to_bytes pkt =
    Bin.to_string (t ~kind:pkt.kind) pkt |> Bytes.unsafe_of_string
end

let input ipv4 pkt payload =
  let dst = pkt.IPv4.src in
  match Packet.decode payload with
  | Error _ ->
      Log.err (fun m -> m "Invalid ICMPv4 packet:");
      Log.err (fun m -> m "@[<hov>%a@]" (Hxd_string.pp Hxd.default) payload)
  | Ok (Packet pkt, payload) ->
      begin match pkt.kind with
      | Packet.Echo_request ->
          Log.debug (fun m -> m "Echo request");
          let pkt =
            { Packet.code= 0; kind= Echo_reply; checksum= 0; shdr= pkt.shdr }
          in
          let buf = Packet.to_bytes pkt in
          let chk =
            Utcp.Checksum.digest_strings [ Bytes.unsafe_to_string buf; payload ]
          in
          Bytes.set_uint16_be buf 2 chk;
          let pkt = Bytes.unsafe_to_string buf in
          let pkt = IPv4.Writer.of_strings ipv4 [ pkt; payload ] in
          let result = IPv4.write ipv4 dst ~protocol:1 pkt in
          let err _ =
            Log.err (fun m -> m "Impossible to send ICMPv4 echo-reply packet")
          in
          let _ = Result.map_error err result in
          ()
      | _ -> Log.debug (fun m -> m "Ignore ICMPv4 packet")
      end

type t = {
    mutex: Miou.Mutex.t
  ; condition: Miou.Condition.t
  ; queue: (IPv4.packet * string) Queue.t
  ; ipv4: IPv4.t
  ; orphans: unit Miou.orphans
}

let rec clean orphans =
  match Miou.care orphans with
  | None | Some None -> ()
  | Some (Some prm) -> (
      match Miou.await prm with
      | Ok () -> clean orphans
      | Error exn ->
          Log.err (fun m ->
              m "Unexpected exception from an ICMPv4 task: %s"
                (Printexc.to_string exn));
          clean orphans)

let rec handler t =
  clean t.orphans;
  let todo =
    Miou.Mutex.protect t.mutex @@ fun () ->
    while Queue.is_empty t.queue do
      Miou.Condition.wait t.condition t.mutex
    done;
    let todo = Queue.create () in
    Queue.transfer t.queue todo;
    todo
  in
  let fn (pkt, payload) =
    ignore (Miou.async ~orphans:t.orphans @@ fun () -> input t.ipv4 pkt payload)
  in
  Queue.iter fn todo; handler t

type daemon = unit Miou.t * t

let handler ipv4 : daemon =
  let mutex = Miou.Mutex.create () in
  let condition = Miou.Condition.create () in
  let queue = Queue.create () in
  let orphans = Miou.orphans () in
  let t = { mutex; condition; queue; ipv4; orphans } in
  (Miou.async (fun () -> handler t), t)

let kill (prm, _) = Miou.cancel prm

let transfer (_, t) (pkt, payload) =
  let payload =
    match payload with
    | IPv4.Slice bstr -> Slice_bstr.to_string bstr
    | IPv4.String str -> str
  in
  Miou.Mutex.protect t.mutex @@ fun () ->
  Queue.push (pkt, payload) t.queue;
  Miou.Condition.signal t.condition