Source file dhcp_client.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
type state = | Selecting of Dhcp_wire.pkt
| Requesting of (Dhcp_wire.pkt * Dhcp_wire.pkt)
| Bound of Dhcp_wire.pkt
| Renewing of (Dhcp_wire.pkt * Dhcp_wire.pkt)
| Rebinding of (Dhcp_wire.pkt * Dhcp_wire.pkt option * Dhcp_wire.pkt)
type t = {
srcmac : Macaddr.t;
request_options : Dhcp_wire.option_code list;
options : Dhcp_wire.dhcp_option list;
state : state;
}
module Constants = struct
open Dhcp_wire
let htype = Ethernet_10mb
let hlen = 6
let hops = 0
let sname = ""
let file = ""
end
let default_requests =
Dhcp_wire.([
SUBNET_MASK;
ROUTERS;
DNS_SERVERS;
DOMAIN_NAME;
PERFORM_ROUTER_DISC;
STATIC_ROUTES;
VENDOR_SPECIFIC;
NETBIOS_NAME_SERVERS;
NETBIOS_NODE;
NETBIOS_SCOPE;
CLASSLESS_STATIC_ROUTE;
PRIVATE_CLASSLESS_STATIC_ROUTE;
WEB_PROXY_AUTO_DISC;
])
let pp fmt p =
let pp_state fmt = function
| Selecting pkt -> Format.fprintf fmt "SELECTING. Generated %a" Dhcp_wire.pp_pkt pkt
| Requesting (received, sent) ->
Format.fprintf fmt
"REQUESTING. Received %a, and generated response %a"
Dhcp_wire.pp_pkt received Dhcp_wire.pp_pkt sent
| Bound pkt -> Format.fprintf fmt "BOUND. Received %a" Dhcp_wire.pp_pkt pkt
| Renewing (ack, request) ->
Format.fprintf fmt
"RENEWING. Have lease %a, generated request %a"
Dhcp_wire.pp_pkt ack Dhcp_wire.pp_pkt request
| Rebinding (ack, _renew_request, request) ->
Format.fprintf fmt
"REBINDING. Have lease %a, generated request %a"
Dhcp_wire.pp_pkt ack Dhcp_wire.pp_pkt request
in
Format.fprintf fmt "%a: %a" Macaddr.pp p.srcmac pp_state p.state
let lease {state; _} = match state with
| Bound dhcpack | Renewing (dhcpack, _) | Rebinding (dhcpack, _, _) -> Some dhcpack
| Requesting _ | Selecting _ -> None
let most_recent_xid { state; _ } =
match state with
| Selecting p -> p.xid
| Requesting (_i, o) -> o.xid
| Bound a -> a.xid
| Renewing (_i, o) -> o.xid
| Rebinding (_i, _o, o') -> o'.xid
let xid_matches {state; _} xid =
let open Dhcp_wire in
let is_match xid' = Int32.equal xid xid' in
match state with
| Selecting p -> is_match p.xid
| Requesting (_i, o) -> is_match o.xid
| Bound a -> is_match a.xid
| Renewing (_i, o) -> is_match o.xid
| Rebinding (_i, o, o') ->
Option.fold o ~none:false ~some:(fun o -> is_match o.xid)
|| is_match o'.xid
let make_request ?(srcip = Ipaddr.V4.any) ?(dstip = Ipaddr.V4.broadcast) ?(ciaddr = Ipaddr.V4.any)
~xid ~chaddr ~srcmac ~siaddr ~options () =
let open Dhcp_wire in
Constants.({
htype; hlen; hops; sname; file;
xid;
chaddr;
srcport = Dhcp_wire.client_port;
dstport = Dhcp_wire.server_port;
srcmac;
srcip;
dstmac = Macaddr.broadcast;
dstip;
op = BOOTREQUEST;
options;
secs = 0;
flags = Broadcast;
ciaddr;
yiaddr = Ipaddr.V4.any;
siaddr;
giaddr = Ipaddr.V4.any;
})
let offer (t : t) ~xid ~chaddr ~server_ip ~request_ip ~offer_options =
let open Dhcp_wire in
let options = [
Message_type DHCPREQUEST;
Request_ip request_ip;
] @ t.options
in
let options =
match Dhcp_wire.find_server_identifier offer_options with
| None -> options
| Some server_ip ->
Server_identifier server_ip :: options
in
let options =
match t.request_options with
| [] -> options
| _::_ -> (Parameter_requests t.request_options) :: options
in
make_request ~xid ~chaddr ~srcmac:t.srcmac ~siaddr:server_ip ~options:options ()
let renew_request ?siaddr (t : t) ~ciaddr ~xid ~chaddr =
let open Dhcp_wire in
let options = [
Message_type DHCPREQUEST;
] @ t.options in
let options =
match t.request_options with
| [] -> options
| _::_ -> (Parameter_requests t.request_options) :: options
in
let dstip, siaddr =
match siaddr with
| None -> None, Ipaddr.V4.any
| Some siaddr -> Some siaddr, siaddr
in
make_request ~srcip:ciaddr ?dstip ~ciaddr ~xid ~chaddr ~srcmac:t.srcmac ~siaddr ~options ()
let create ?(options = []) ?requests xid srcmac =
let open Constants in
let open Dhcp_wire in
let requests = match requests with
| None | Some [] -> default_requests
| Some requests -> requests
in
let pkt = {
htype; hlen; hops; sname; file;
srcmac;
dstmac = Macaddr.broadcast;
srcip = Ipaddr.V4.any;
dstip = Ipaddr.V4.broadcast;
srcport = client_port;
dstport = server_port;
op = BOOTREQUEST;
xid;
secs = 0;
flags = Broadcast;
ciaddr = Ipaddr.V4.any;
yiaddr = Ipaddr.V4.any;
siaddr = Ipaddr.V4.any;
giaddr = Ipaddr.V4.any;
chaddr = srcmac;
options = [
Message_type DHCPDISCOVER;
Client_id (Hwaddr srcmac);
Parameter_requests requests;
] @ options;
} in
{srcmac; request_options = requests; options; state = Selecting pkt}, pkt
let input t buf =
let open Dhcp_wire in
match pkt_of_buf buf (Cstruct.length buf) with
| Error `Not_dhcp -> `Not_dhcp
| Error `Msg _ -> `Noop
| Ok incoming ->
if xid_matches t incoming.xid then begin
match find_message_type incoming.options, t.state with
| None, _ -> `Noop
| Some DHCPOFFER, Selecting dhcpdiscover ->
let dhcprequest = offer t ~server_ip:incoming.siaddr
~request_ip:incoming.yiaddr
~offer_options:incoming.options
~xid:dhcpdiscover.xid
~chaddr:dhcpdiscover.chaddr in
`Response ({t with state = Requesting (incoming, dhcprequest)},
dhcprequest)
| Some DHCPOFFER, _ ->
`Noop
| Some DHCPACK, Renewing _
| Some DHCPACK, Rebinding _
| Some DHCPACK, Requesting _ -> `New_lease ({t with state = Bound incoming}, incoming)
| Some DHCPNAK, Requesting _
| Some DHCPNAK, Renewing _
| Some DHCPNAK, Rebinding _ ->
`Response (create ~options:t.options ~requests:t.request_options (most_recent_xid t) t.srcmac)
| Some DHCPACK, Selecting _
| Some DHCPACK, Bound _ ->
`Noop
| Some DHCPDISCOVER, _ | Some DHCPDECLINE, _ | Some DHCPRELEASE, _
| Some DHCPINFORM, _ | Some DHCPREQUEST, _ ->
`Noop
| Some DHCPNAK, Selecting _| Some DHCPNAK, Bound _ -> `Noop
| Some DHCPLEASEQUERY, _ | Some DHCPLEASEUNASSIGNED, _
| Some DHCPLEASEUNKNOWN, _ | Some DHCPLEASEACTIVE, _
| Some DHCPBULKLEASEQUERY, _ | Some DHCPLEASEQUERYDONE, _ ->
`Noop
| Some DHCPFORCERENEW, _ -> `Noop
end else `Noop
let renew t = match t.state with
| Selecting _ | Requesting _ | Rebinding _ -> `Noop
| Renewing (_lease, request) -> `Response (t, request)
| Bound lease ->
let request = renew_request t ~ciaddr:lease.yiaddr ~xid:lease.xid ~chaddr:lease.chaddr ~siaddr:lease.siaddr in
let state = Renewing (lease, request) in
`Response ({t with state = state}, request)
let rebind t = match t.state with
| Selecting _ | Requesting _ -> `Noop
| Rebinding (_lease, _renew_request, request) -> `Response (t, request)
| Bound lease ->
let request = renew_request t ~ciaddr:lease.yiaddr ~xid:lease.xid ~chaddr:lease.chaddr in
let state = Rebinding (lease, None, request) in
`Response ({ t with state }, request)
| Renewing (lease, old_renew_request) ->
let request = renew_request t ~ciaddr:lease.yiaddr ~xid:lease.xid ~chaddr:lease.chaddr in
let state = Rebinding (lease, Some old_renew_request, request) in
`Response ({ t with state }, request)