-
Notifications
You must be signed in to change notification settings - Fork 1
/
dhcp.ml
322 lines (302 loc) · 13.1 KB
/
dhcp.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
(* vim:sw=4 ts=4 sts=4 expandtab spell spelllang=en
*)
(* Copyright 2012, Cedric Cellier
*
* This file is part of RobiNet.
*
* RobiNet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RobiNet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with RobiNet. If not, see <http://www.gnu.org/licenses/>.
*)
(** Dynamic Host Configuration Protocol. *)
open Batteries
open Bitstring
open Tools
let debug = true
(** {2 Opcodes, types, etc} *)
let bootrequest = 1
let bootreply = 2
type opcode = BootRequest | BootReply
module MsgType = struct
module Inner = struct
type t = int
let to_string = function
| 1 -> "DISCOVER"
| 2 -> "OFFER"
| 3 -> "REQUEST"
| 4 -> "DECLINE"
| 5 -> "ACK"
| 6 -> "NACK"
| 7 -> "RELEASE"
| 8 -> "INFORM"
| _ -> should_not_happen ()
let is_valid x = x >= 1 && x <= 8
let repl_tag = "code"
end
include Private.Make (Inner)
let discover = o 1
let offer = o 2
let request = o 3
let decline = o 4
let ack = o 5
let nack = o 6
let release = o 7
let inform = o 8
let rec random () =
let p = randi 3 + 1 in
if Inner.is_valid p then p else random ()
end
(** {2 DHCP messages} *)
module Pdu =
struct
(*$< Pdu *)
type t =
{ op : opcode ;
htype : Arp.HwType.t ;
hlen : int ; hops : int ;
xid : int32 ;
secs : int ; broadcast : bool ;
ciaddr : Ip.Addr.t ; yiaddr : Ip.Addr.t ;
siaddr : Ip.Addr.t ; giaddr : Ip.Addr.t ;
chaddr : bitstring ;
sname : string ;
file : string ;
(* Bootp options *)
mutable msg_type : MsgType.t option ;
mutable subnet_mask : Ip.Addr.t option ;
mutable router : Ip.Addr.t option ;
mutable ntp_server : Ip.Addr.t option ;
mutable smtp_server : Ip.Addr.t option ;
mutable pop3_server : Ip.Addr.t option ;
mutable name_server : Ip.Addr.t option ;
mutable client_name : string option ;
mutable search_sfx : string option ;
mutable lease_time : int32 option ; (* in seconds *)
mutable server_id : Ip.Addr.t option ;
mutable requested_ip : Ip.Addr.t option ;
mutable message : string option ;
mutable client_id : bitstring option ;
mutable request_list : string option }
let rec unpack_options t bits = match%bitstring bits with
| {| 0 : 8 ;
rest : -1 : bitstring |} -> unpack_options t rest
| {| 255 : 8 |} -> true
| {| 1 : 8 ; 4 : 8 ; subnet_mask : 32 ;
rest : -1 : bitstring |} ->
t.subnet_mask <- Some (Ip.Addr.o32 subnet_mask) ;
unpack_options t rest
| {| 3 : 8 ; len : 8 : check (len >= 4) ; ips : 8*len : bitstring ;
rest : -1 : bitstring |} ->
t.router <- Some (Ip.Addr.of_bitstring (takebits 32 ips)) ;
unpack_options t rest
| {| 42 : 8 ; len : 8 : check (len >= 4) ; ips : 8*len : bitstring ;
rest : -1 : bitstring |} ->
t.ntp_server <- Some (Ip.Addr.of_bitstring (takebits 32 ips)) ;
unpack_options t rest
| {| 69 : 8 ; len : 8 : check (len >= 4 && len land 3 = 0) ; ips : 8*len : bitstring ;
rest : -1 : bitstring |} ->
t.smtp_server <- Some (Ip.Addr.of_bitstring (takebits 32 ips)) ;
unpack_options t rest
| {| 70 : 8 ; len : 8 : check (len >= 4 && len land 3 = 0) ; ips : 8*len : bitstring ;
rest : -1 : bitstring |} ->
t.pop3_server <- Some (Ip.Addr.of_bitstring (takebits 32 ips)) ;
unpack_options t rest
| {| 6 : 8 ; len : 8 : check (len >= 4) ; ips : 8*len : bitstring ;
rest : -1 : bitstring |} ->
t.name_server <- Some (Ip.Addr.of_bitstring (takebits 32 ips)) ;
unpack_options t rest
| {| 12 : 8 ; len : 8 : check (len >= 1) ; name : 8*len : string ;
rest : -1 : bitstring |} ->
t.client_name <- Some name ;
unpack_options t rest
| {| 15 : 8 ; len : 8 : check (len >= 1) ; sfx : 8*len : string ;
rest : -1 : bitstring |} ->
t.search_sfx <- Some sfx ;
unpack_options t rest
| {| 50 : 8 ; 4 : 8 ; req_ip : 32 ;
rest : -1 : bitstring |} ->
t.requested_ip <- Some (Ip.Addr.o32 req_ip) ;
unpack_options t rest
| {| 51 : 8 ; 4 : 8 ; lease : 32 ;
rest : -1 : bitstring |} ->
t.lease_time <- Some lease ;
unpack_options t rest
| {| 53 : 8 ; 1 : 8 ; msg_type : 8 : check (msg_type > 0 && msg_type < 9) ;
rest : -1 : bitstring |} ->
t.msg_type <- Some (MsgType.o msg_type) ;
unpack_options t rest
| {| 54 : 8 ; 4 : 8 ; ip : 32 ;
rest : -1 : bitstring |} ->
t.server_id <- Some (Ip.Addr.o32 ip) ;
unpack_options t rest
| {| 55 : 8 ; len : 8 : check (len > 0) ; params : 8*len : string ;
rest : -1 : bitstring |} ->
t.request_list <- Some params ;
unpack_options t rest
| {| 56 : 8 ; len : 8 : check (len > 0) ; msg : 8*len : string ;
rest : -1 : bitstring |} ->
t.message <- Some msg ;
unpack_options t rest
| {| 61 : 8 ; len : 8 : check (len >= 2) ; id : 8*len : bitstring ;
rest : -1 : bitstring |} ->
t.client_id <- Some id ;
unpack_options t rest
(* FIXME: IP Layer parameters setting could be interesting to get/set via DHCP.
At least netmask *)
(* FIXME: handle option overload of file/sname fields with more options *)
| {| _ : 8 ; len : 8 ; _ : 8*len ; rest : -1 : bitstring |} ->
unpack_options t rest
| {| _ |} -> false
let unpack bits = match%bitstring bits with
| {| op : 8 : check (op = bootrequest || op = bootreply) ;
htype : 8 ; hlen : 8 ; hops : 8 ;
xid : 32 ; secs : 16 ;
flags : 16 : check (flags land 0x7fff = 0) ;
ciaddr : 32 : bitstring ;
yiaddr : 32 : bitstring ;
siaddr : 32 : bitstring ;
giaddr : 32 : bitstring ;
chaddr : 16*8 : bitstring ;
sname : 64*8 : string ;
file : 128*8 : string ;
0x63825363l : 32 ;
options : -1 : bitstring |} ->
let t = { op = if op = bootrequest then BootRequest else BootReply ;
htype = Arp.HwType.o htype ; hlen ; hops ; xid ;
secs ; broadcast = flags land 0x8000 = 0x8000 ;
ciaddr = Ip.Addr.of_bitstring ciaddr ;
yiaddr = Ip.Addr.of_bitstring yiaddr ;
siaddr = Ip.Addr.of_bitstring siaddr ;
giaddr = Ip.Addr.of_bitstring giaddr ;
chaddr = takebytes hlen chaddr ; sname ; file ;
subnet_mask = None ;
router = None ;
ntp_server = None ;
smtp_server = None ;
pop3_server = None ;
name_server = None ;
client_name = None ;
search_sfx = None ;
lease_time = None ;
msg_type = None ;
server_id = None ;
requested_ip = None ;
message = None ;
client_id = None ;
request_list = None } in
if unpack_options t options then Some t
else err "Dhcp: Cannot decode options"
| {| _ |} -> err "Dhcp: Not DHCP"
let pack_options t =
let may_pack_msgtyp t v = Option.map (fun (v : MsgType.t) -> let%bitstring b = {| t : 8 ; 1 : 8 ; (v :> int) : 8 |} in b) v
and may_pack_int32 t v = Option.map (fun v -> let%bitstring b = {| t : 8 ; 4 : 8 ; v : 32 |} in b) v
and may_pack_ip t v = Option.map (fun (v : Ip.Addr.t) -> let%bitstring b = {| t : 8 ; 4 : 8 ; (Ip.Addr.to_int32 v) : 32 |} in b) v
and may_pack_string t v = Option.map (fun v -> let%bitstring b = {| t : 8 ; String.length v : 8 ; v : -1 : string |} in b) v
and may_pack_bits t v = Option.map (fun v -> let%bitstring b = {| t : 8 ; bytelength v : 8 ; v : -1 : bitstring |} in b) v
in
List.enum [ may_pack_msgtyp 53 t.msg_type ;
may_pack_ip 1 t.subnet_mask ; (* must appear before router *)
may_pack_ip 3 t.router ;
may_pack_ip 42 t.ntp_server ;
may_pack_ip 69 t.smtp_server ;
may_pack_ip 70 t.pop3_server ;
may_pack_ip 6 t.name_server ;
may_pack_string 12 t.client_name ;
may_pack_string 15 t.search_sfx ;
may_pack_int32 51 t.lease_time ;
may_pack_ip 50 t.requested_ip ;
may_pack_ip 54 t.server_id ;
may_pack_string 56 t.message ;
may_pack_bits 61 t.client_id ;
may_pack_string 55 t.request_list ;
Some (let%bitstring b = {| 255 : 8 |} in b) ] //@
identity |>
List.of_enum |>
Bitstring.concat
let pack t =
let string_extend str len =
let l = String.length str in
if l >= len then String.sub str 0 len
else str ^ (String.make (len-l) (Char.chr 0))
in
let%bitstring b = {|
match t.op with BootRequest -> 1 | BootReply -> 2 : 8 ;
(t.htype :> int) : 8 ; t.hlen : 8 ; t.hops : 8 ;
t.xid : 32 ;
t.secs : 16 ; if t.broadcast then 0x8000 else 0 : 16 ;
Ip.Addr.to_bitstring t.ciaddr : -1 : bitstring ;
Ip.Addr.to_bitstring t.yiaddr : -1 : bitstring ;
Ip.Addr.to_bitstring t.siaddr : -1 : bitstring ;
Ip.Addr.to_bitstring t.giaddr : -1 : bitstring ;
extendbytes 16 t.chaddr : -1 : bitstring ;
string_extend t.sname 64 : 64*8 : string ;
string_extend t.file 128 : 128*8 : string ;
0x63825363l : 32 ;
pack_options t : -1 : bitstring |} in b
let make_base ?(mac=Eth.Addr.zero) ?xid ?name ?(yiaddr=Ip.Addr.zero) msg_type =
let xid = may_default xid (fun () -> Random.int32 Int32.max_int) in
{ op = BootRequest ;
htype = Arp.HwType.eth ;
hlen = 6 ; hops = 0 ;
xid ;
secs = 0 ; broadcast = false ;
ciaddr = Ip.Addr.zero ;
yiaddr ;
siaddr = Ip.Addr.zero ;
giaddr = Ip.Addr.zero ;
chaddr = extendbytes 16 (mac :> bitstring) ;
sname = "" ; file = "" ;
msg_type = Some msg_type ;
subnet_mask = None ; router = None ;
ntp_server = None ; smtp_server = None ;
pop3_server = None ; name_server = None ;
client_name = name ; requested_ip = None ;
search_sfx = None ; lease_time = None ;
server_id = None ; message = None ;
client_id = None ; request_list = None }
let make_discover ?(mac=Eth.Addr.zero) ?xid ?name () =
let t = make_base ~mac ?xid ?name MsgType.discover in
let%bitstring id = {|
(Arp.HwType.eth :> int) : 8 ;
(mac :> bitstring) : 6*8 : bitstring |} in
t.client_id <- Some id ;
t.request_list <- Some "\001\003\006\012\015\028\051\058\119" ;
t
let make_offer ?(mac=Eth.Addr.zero) ?xid yiaddr client_id =
let t = make_base ~mac ?xid ~yiaddr MsgType.offer in
t.client_id <- client_id ;
t
let make_request ?(mac=Eth.Addr.zero) ?xid ?name yiaddr server_id =
let t = make_base ~mac ?xid ?name MsgType.request in
let%bitstring id = {|
(Arp.HwType.eth :> int) : 8 ;
(mac :> bitstring) : 6*8 : bitstring |} in
t.client_id <- Some id ;
t.request_list <- Some "\001\003\006\012\015\028\051\058\119" ;
t.requested_ip <- Some yiaddr ;
t.server_id <- server_id ;
t
let make_ack ?(mac=Eth.Addr.zero) ?xid yiaddr client_id =
let t = make_base ~mac ?xid ~yiaddr MsgType.ack in
t.client_id <- client_id ;
t
let random () =
let xid = rand32 () and name = randstr 8 in
if randb () then
make_discover ~xid ~name ()
else
make_request ~xid ~name (Ip.Addr.random ()) (if randb () then Some (Ip.Addr.random ()) else None)
(*$Q pack
(Q.make (fun _ -> random () |> pack)) (fun t -> t = pack (Option.get (unpack t)))
*)
(*$>*)
end