summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Universal/Network/PxeDhcp4Dxe/PxeDhcp4RenewRebind.c
blob: 193bb1880aed8e1b0023a44c78c6b53fd25ed2b0 (plain)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/** @file

Copyright (c) 2004, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

Module Name:
  PxeDhcp4RenewRebind.c

Abstract:


**/


#include "PxeDhcp4.h"

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/**
  Parameters:

  @return -2 = ignore, stop waiting
  @return -1 = ignore, keep waiting
  @return 0 = accept, keep waiting
  @return 1 = accept, stop waiting

**/
STATIC
INTN
acknak_verify (
  IN PXE_DHCP4_PRIVATE_DATA *Private,
  IN DHCP4_PACKET           *tx_pkt,
  IN DHCP4_PACKET           *rx_pkt,
  IN UINTN                  rx_pkt_size
  )
{
  EFI_STATUS  efi_status;
  DHCP4_OP    *msg_type_op;
  DHCP4_OP    *srvid_op;
  DHCP4_OP    *renew_op;
  DHCP4_OP    *rebind_op;
  DHCP4_OP    *lease_time_op;
  UINT32      magik;

  //
  // Verify parameters.  Unused parameters are also touched
  // to make the compiler happy.
  //
  ASSERT (Private);
  ASSERT (rx_pkt);

  if (Private == NULL || rx_pkt == NULL) {
    return -2;
  }

  tx_pkt      = tx_pkt;
  rx_pkt_size = rx_pkt_size;

  //
  // This must be a DHCP Ack message.
  //
  magik = htonl (DHCP4_MAGIK_NUMBER);

  if (CompareMem (&rx_pkt->dhcp4.magik, &magik, 4)) {
    return -1;
  }

  efi_status = find_opt (rx_pkt, DHCP4_MESSAGE_TYPE, 0, &msg_type_op);

  if (EFI_ERROR (efi_status)) {
    return -1;
  }

  if (msg_type_op->len != 1) {
    return -1;
  }

  if (msg_type_op->data[0] != DHCP4_MESSAGE_TYPE_ACK) {
    return -1;
  }
  //
  // There must be a server identifier.
  //
  efi_status = find_opt (rx_pkt, DHCP4_SERVER_IDENTIFIER, 0, &srvid_op);

  if (EFI_ERROR (efi_status)) {
    return -1;
  }

  if (srvid_op->len != 4) {
    return -1;
  }
  //
  // There should be a renewal time.
  // If there is not, we will default to the 7/8 of the rebinding time.
  //
  efi_status = find_opt (rx_pkt, DHCP4_RENEWAL_TIME, 0, &renew_op);

  if (EFI_ERROR (efi_status)) {
    renew_op = NULL;
  } else if (renew_op->len != 4) {
    renew_op = NULL;
  }
  //
  // There should be a rebinding time.
  // If there is not, we will default to 7/8 of the lease time.
  //
  efi_status = find_opt (rx_pkt, DHCP4_REBINDING_TIME, 0, &rebind_op);

  if (EFI_ERROR (efi_status)) {
    rebind_op = NULL;
  } else if (rebind_op->len != 4) {
    rebind_op = NULL;
  }
  //
  // There should be a lease time.
  // If there is not, we will default to one week.
  //
  efi_status = find_opt (rx_pkt, DHCP4_LEASE_TIME, 0, &lease_time_op);

  if (EFI_ERROR (efi_status)) {
    lease_time_op = NULL;
  } else if (lease_time_op->len != 4) {
    lease_time_op = NULL;
  }
  //
  // Packet looks good.  Double check the renew, rebind and lease times.
  //
  CopyMem (&Private->ServerIp, srvid_op->data, 4);

  if (renew_op != NULL) {
    CopyMem (&Private->RenewTime, renew_op->data, 4);
    Private->RenewTime = htonl (Private->RenewTime);
  } else {
    Private->RenewTime = 0;
  }

  if (rebind_op != NULL) {
    CopyMem (&Private->RebindTime, rebind_op->data, 4);
    Private->RebindTime = htonl (Private->RebindTime);
  } else {
    Private->RebindTime = 0;
  }

  if (lease_time_op != NULL) {
    CopyMem (&Private->LeaseTime, lease_time_op->data, 4);
    Private->LeaseTime = htonl (Private->LeaseTime);
  } else {
    Private->LeaseTime = 0;
  }

  if (Private->LeaseTime < 60) {
    Private->LeaseTime = 7 * 86400;
  }

  if (Private->RebindTime < 52 || Private->RebindTime >= Private->LeaseTime) {
    Private->RebindTime = Private->LeaseTime / 2 + Private->LeaseTime / 4 + Private->LeaseTime / 8;
  }

  if (Private->RenewTime < 45 || Private->RenewTime >= Private->RebindTime) {
    Private->RenewTime = Private->RebindTime / 2 + Private->RebindTime / 4 + Private->RebindTime / 8;
  }

  return 1;
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
STATIC
EFI_STATUS
EFIAPI
renew_rebind (
  IN EFI_PXE_DHCP4_PROTOCOL *This,
  IN UINTN                  seconds_timeout,
  IN BOOLEAN                renew
  )
{
  PXE_DHCP4_PRIVATE_DATA  *Private;
  EFI_IP_ADDRESS          ServerIp;
  EFI_IP_ADDRESS          client_ip;
  EFI_IP_ADDRESS          subnet_mask;
  EFI_IP_ADDRESS          gateway_ip;
  DHCP4_PACKET            Request;
  DHCP4_PACKET            AckNak;
  DHCP4_OP                *op;
  EFI_STATUS              efi_status;

  //
  // Check for invalid parameters.
  //
  if (This == NULL || seconds_timeout < DHCP4_MIN_SECONDS || seconds_timeout > DHCP4_MAX_SECONDS) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Check for proper protocol state.
  //
  if (This->Data == NULL) {
    return EFI_NOT_STARTED;
  }

  if (!This->Data->SelectCompleted) {
    return EFI_NOT_READY;
  }

  if (This->Data->IsBootp) {
    return EFI_SUCCESS;
  }

  if (!This->Data->IsAck) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Get pointer to instance data.
  //
  Private = PXE_DHCP4_PRIVATE_DATA_FROM_THIS (This);

  if (Private == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  if (Private->PxeBc == NULL) {
    return EFI_DEVICE_ERROR;
  }
  //
  // Copy Discover packet to temporary request packet
  // to be used for Renew/Rebind operation.
  //
  CopyMem (&Request, &This->Data->Discover, sizeof (DHCP4_PACKET));

  CopyMem (&Request.dhcp4.ciaddr, &This->Data->AckNak.dhcp4.yiaddr, 4);

  Request.dhcp4.flags = 0;  /* Reply does not need to be broadcast. */

  //
  // Change message type from discover to request.
  //
  efi_status = find_opt (&Request, DHCP4_MESSAGE_TYPE, 0, &op);

  if (EFI_ERROR (efi_status)) {
    return EFI_INVALID_PARAMETER;
  }

  if (op->len != 1) {
    return EFI_INVALID_PARAMETER;
  }

  op->data[0] = DHCP4_MESSAGE_TYPE_REQUEST;

  //
  // Need a subnet mask.
  //
  efi_status = find_opt (
                &This->Data->AckNak,
                DHCP4_SUBNET_MASK,
                0,
                &op
                );

  if (EFI_ERROR (efi_status)) {
    return EFI_INVALID_PARAMETER;
  }

  if (op->len != 4) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (&subnet_mask, sizeof (EFI_IP_ADDRESS));
  CopyMem (&subnet_mask, op->data, 4);

  //
  // Need a server IP address (renew) or a broadcast
  // IP address (rebind).
  //
  ZeroMem (&gateway_ip, sizeof (EFI_IP_ADDRESS));

  if (renew) {
    efi_status = find_opt (
                  &This->Data->AckNak,
                  DHCP4_SERVER_IDENTIFIER,
                  0,
                  &op
                  );

    if (EFI_ERROR (efi_status)) {
      return EFI_INVALID_PARAMETER;
    }

    if (op->len != 4) {
      return EFI_INVALID_PARAMETER;
    }

    ZeroMem (&ServerIp, sizeof (EFI_IP_ADDRESS));
    CopyMem (&ServerIp, op->data, 4);

    //
    //
    //
    if (CompareMem (&This->Data->AckNak.dhcp4.giaddr, &gateway_ip, 4)) {
      CopyMem (&gateway_ip, &This->Data->AckNak.dhcp4.giaddr, 4);
    }
  } else {
    SetMem (&ServerIp, sizeof (EFI_IP_ADDRESS), 0xFF);
  }
  //
  // Need a client IP address.
  //
  ZeroMem (&client_ip, sizeof (EFI_IP_ADDRESS));
  CopyMem (&client_ip, &Request.dhcp4.ciaddr, 4);

  //
  //
  //
  efi_status = gBS->HandleProtocol (
                      Private->Handle,
                      &gEfiPxeDhcp4CallbackProtocolGuid,
                      (VOID *) &Private->callback
                      );

  if (EFI_ERROR (efi_status)) {
    Private->callback = NULL;
  }

  Private->function = renew ? EFI_PXE_DHCP4_FUNCTION_RENEW : EFI_PXE_DHCP4_FUNCTION_REBIND;

  //
  // Transimit DHCP request and wait for DHCP ack...
  //
  efi_status = tx_rx_udp (
                Private,
                &ServerIp,
                &gateway_ip,
                &client_ip,
                &subnet_mask,
                &Request,
                &AckNak,
                &acknak_verify,
                seconds_timeout
                );

  if (EFI_ERROR (efi_status)) {
    Private->callback = NULL;
    return efi_status;
  }
  //
  // Copy server identifier, renewal time and rebinding time
  // from temporary ack/nak packet into cached ack/nak packet.
  //
  efi_status = find_opt (
                &This->Data->AckNak,
                DHCP4_SERVER_IDENTIFIER,
                0,
                &op
                );

  if (!EFI_ERROR (efi_status)) {
    if (op->len == 4) {
      CopyMem (op->data, &Private->ServerIp, 4);
    }
  }

  efi_status = find_opt (&This->Data->AckNak, DHCP4_RENEWAL_TIME, 0, &op);

  if (!EFI_ERROR (efi_status)) {
    if (op->len == 4) {
      CopyMem (op->data, &Private->RenewTime, 4);
    }
  }

  efi_status = find_opt (&This->Data->AckNak, DHCP4_REBINDING_TIME, 0, &op);

  if (!EFI_ERROR (efi_status)) {
    if (op->len == 4) {
      CopyMem (op->data, &Private->RebindTime, 4);
    }
  }

  Private->callback = NULL;
  return efi_status;
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
EFI_STATUS
EFIAPI
PxeDhcp4Renew (
  IN EFI_PXE_DHCP4_PROTOCOL *This,
  IN UINTN                  seconds_timeout
  )
{
  return renew_rebind (This, seconds_timeout, TRUE);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
EFI_STATUS
EFIAPI
PxeDhcp4Rebind (
  IN EFI_PXE_DHCP4_PROTOCOL *This,
  IN UINTN                  seconds_timeout
  )
{
  return renew_rebind (This, seconds_timeout, FALSE);
}

/* eof - PxeDhcp4RenewRebind.c */