]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Io.c
MdeModulePkg: Addressing TCP Window Retraction when window scale factor is used.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Io.c
... / ...
CommitLineData
1/** @file\r
2 EFI DHCP protocol implementation.\r
3 \r
4Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15\r
16#include "Dhcp4Impl.h"\r
17\r
18UINT32 mDhcp4DefaultTimeout[4] = { 4, 8, 16, 32 };\r
19\r
20\r
21/**\r
22 Send an initial DISCOVER or REQUEST message according to the\r
23 DHCP service's current state.\r
24\r
25 @param[in] DhcpSb The DHCP service instance\r
26\r
27 @retval EFI_SUCCESS The request has been sent\r
28 @retval other Some error occurs when sending the request.\r
29\r
30**/\r
31EFI_STATUS\r
32DhcpInitRequest (\r
33 IN DHCP_SERVICE *DhcpSb\r
34 )\r
35{\r
36 EFI_STATUS Status;\r
37\r
38 ASSERT ((DhcpSb->DhcpState == Dhcp4Init) || (DhcpSb->DhcpState == Dhcp4InitReboot));\r
39\r
40 //\r
41 // Clear initial time to make sure that elapsed-time is set to 0 for first Discover or REQUEST message.\r
42 //\r
43 DhcpSb->ActiveChild->ElaspedTime= 0;\r
44 \r
45 if (DhcpSb->DhcpState == Dhcp4Init) {\r
46 DhcpSetState (DhcpSb, Dhcp4Selecting, FALSE);\r
47 Status = DhcpSendMessage (DhcpSb, NULL, NULL, DHCP_MSG_DISCOVER, NULL);\r
48\r
49 if (EFI_ERROR (Status)) {\r
50 DhcpSb->DhcpState = Dhcp4Init;\r
51 return Status;\r
52 }\r
53 } else {\r
54 DhcpSetState (DhcpSb, Dhcp4Rebooting, FALSE);\r
55 Status = DhcpSendMessage (DhcpSb, NULL, NULL, DHCP_MSG_REQUEST, NULL);\r
56\r
57 if (EFI_ERROR (Status)) {\r
58 DhcpSb->DhcpState = Dhcp4InitReboot;\r
59 return Status;\r
60 }\r
61 }\r
62\r
63 return EFI_SUCCESS;\r
64}\r
65\r
66\r
67/**\r
68 Call user provided callback function, and return the value the\r
69 function returns. If the user doesn't provide a callback, a\r
70 proper return value is selected to let the caller continue the\r
71 normal process.\r
72\r
73 @param[in] DhcpSb The DHCP service instance\r
74 @param[in] Event The event as defined in the spec\r
75 @param[in] Packet The current packet trigger the event\r
76 @param[out] NewPacket The user's return new packet\r
77\r
78 @retval EFI_NOT_READY Direct the caller to continue collecting the offer.\r
79 @retval EFI_SUCCESS The user function returns success.\r
80 @retval EFI_ABORTED The user function ask it to abort.\r
81\r
82**/\r
83EFI_STATUS\r
84DhcpCallUser (\r
85 IN DHCP_SERVICE *DhcpSb,\r
86 IN EFI_DHCP4_EVENT Event,\r
87 IN EFI_DHCP4_PACKET *Packet, OPTIONAL\r
88 OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL\r
89 )\r
90{\r
91 EFI_DHCP4_CONFIG_DATA *Config;\r
92 EFI_STATUS Status;\r
93\r
94 if (NewPacket != NULL) {\r
95 *NewPacket = NULL;\r
96 }\r
97\r
98 //\r
99 // If user doesn't provide the call back function, return the value\r
100 // that directs the client to continue the normal process.\r
101 // In Dhcp4Selecting EFI_SUCCESS tells the client to stop collecting\r
102 // the offers and select a offer, EFI_NOT_READY tells the client to\r
103 // collect more offers.\r
104 //\r
105 Config = &DhcpSb->ActiveConfig;\r
106\r
107 if (Config->Dhcp4Callback == NULL) {\r
108 if (Event == Dhcp4RcvdOffer) {\r
109 return EFI_NOT_READY;\r
110 }\r
111\r
112 return EFI_SUCCESS;\r
113 }\r
114\r
115 Status = Config->Dhcp4Callback (\r
116 &DhcpSb->ActiveChild->Dhcp4Protocol,\r
117 Config->CallbackContext,\r
118 (EFI_DHCP4_STATE) DhcpSb->DhcpState,\r
119 Event,\r
120 Packet,\r
121 NewPacket\r
122 );\r
123\r
124 //\r
125 // User's callback should only return EFI_SUCCESS, EFI_NOT_READY,\r
126 // and EFI_ABORTED. If it returns values other than those, assume\r
127 // it to be EFI_ABORTED.\r
128 //\r
129 if ((Status == EFI_SUCCESS) || (Status == EFI_NOT_READY)) {\r
130 return Status;\r
131 }\r
132\r
133 return EFI_ABORTED;\r
134}\r
135\r
136\r
137/**\r
138 Notify the user about the operation result.\r
139\r
140 @param DhcpSb DHCP service instance\r
141 @param Which Which notify function to signal\r
142\r
143**/\r
144VOID\r
145DhcpNotifyUser (\r
146 IN DHCP_SERVICE *DhcpSb,\r
147 IN INTN Which\r
148 )\r
149{\r
150 DHCP_PROTOCOL *Child;\r
151\r
152 if ((Child = DhcpSb->ActiveChild) == NULL) {\r
153 return ;\r
154 }\r
155\r
156 if ((Child->CompletionEvent != NULL) &&\r
157 ((Which == DHCP_NOTIFY_COMPLETION) || (Which == DHCP_NOTIFY_ALL))\r
158 ) {\r
159\r
160 gBS->SignalEvent (Child->CompletionEvent);\r
161 Child->CompletionEvent = NULL;\r
162 }\r
163\r
164 if ((Child->RenewRebindEvent != NULL) &&\r
165 ((Which == DHCP_NOTIFY_RENEWREBIND) || (Which == DHCP_NOTIFY_ALL))\r
166 ) {\r
167\r
168 gBS->SignalEvent (Child->RenewRebindEvent);\r
169 Child->RenewRebindEvent = NULL;\r
170 }\r
171}\r
172\r
173\r
174\r
175/**\r
176 Set the DHCP state. If CallUser is true, it will try to notify\r
177 the user before change the state by DhcpNotifyUser. It returns\r
178 EFI_ABORTED if the user return EFI_ABORTED, otherwise, it returns\r
179 EFI_SUCCESS. If CallUser is FALSE, it isn't necessary to test\r
180 the return value of this function.\r
181\r
182 @param DhcpSb The DHCP service instance\r
183 @param State The new DHCP state to change to\r
184 @param CallUser Whether we need to call user\r
185\r
186 @retval EFI_SUCCESS The state is changed\r
187 @retval EFI_ABORTED The user asks to abort the DHCP process.\r
188\r
189**/\r
190EFI_STATUS\r
191DhcpSetState (\r
192 IN OUT DHCP_SERVICE *DhcpSb,\r
193 IN INTN State,\r
194 IN BOOLEAN CallUser\r
195 )\r
196{\r
197 EFI_STATUS Status;\r
198\r
199 if (CallUser) {\r
200 Status = EFI_SUCCESS;\r
201\r
202 if (State == Dhcp4Renewing) {\r
203 Status = DhcpCallUser (DhcpSb, Dhcp4EnterRenewing, NULL, NULL);\r
204\r
205 } else if (State == Dhcp4Rebinding) {\r
206 Status = DhcpCallUser (DhcpSb, Dhcp4EnterRebinding, NULL, NULL);\r
207\r
208 } else if (State == Dhcp4Bound) {\r
209 Status = DhcpCallUser (DhcpSb, Dhcp4BoundCompleted, NULL, NULL);\r
210\r
211 }\r
212\r
213 if (EFI_ERROR (Status)) {\r
214 return Status;\r
215 }\r
216 }\r
217\r
218 //\r
219 // Update the retransmission timer during the state transition.\r
220 // This will clear the retry count. This is also why the rule\r
221 // first transit the state, then send packets.\r
222 //\r
223 if (State == Dhcp4Selecting) {\r
224 DhcpSb->MaxRetries = DhcpSb->ActiveConfig.DiscoverTryCount;\r
225 } else {\r
226 DhcpSb->MaxRetries = DhcpSb->ActiveConfig.RequestTryCount;\r
227 }\r
228\r
229 if (DhcpSb->MaxRetries == 0) {\r
230 DhcpSb->MaxRetries = 4;\r
231 }\r
232\r
233 DhcpSb->CurRetry = 0;\r
234 DhcpSb->PacketToLive = 0;\r
235 DhcpSb->LastTimeout = 0;\r
236 DhcpSb->DhcpState = State;\r
237 return EFI_SUCCESS;\r
238}\r
239\r
240\r
241/**\r
242 Set the retransmit timer for the packet. It will select from either\r
243 the discover timeouts/request timeouts or the default timeout values.\r
244\r
245 @param DhcpSb The DHCP service instance.\r
246\r
247**/\r
248VOID\r
249DhcpSetTransmitTimer (\r
250 IN OUT DHCP_SERVICE *DhcpSb\r
251 )\r
252{\r
253 UINT32 *Times;\r
254\r
255 ASSERT (DhcpSb->MaxRetries > DhcpSb->CurRetry);\r
256\r
257 if (DhcpSb->DhcpState == Dhcp4Selecting) {\r
258 Times = DhcpSb->ActiveConfig.DiscoverTimeout;\r
259 } else {\r
260 Times = DhcpSb->ActiveConfig.RequestTimeout;\r
261 }\r
262\r
263 if (Times == NULL) {\r
264 Times = mDhcp4DefaultTimeout;\r
265 }\r
266\r
267 DhcpSb->PacketToLive = Times[DhcpSb->CurRetry];\r
268 DhcpSb->LastTimeout = DhcpSb->PacketToLive;\r
269\r
270 return;\r
271}\r
272\r
273/**\r
274 Compute the lease. If the server grants a permanent lease, just\r
275 process it as a normal timeout value since the lease will last\r
276 more than 100 years.\r
277\r
278 @param DhcpSb The DHCP service instance\r
279 @param Para The DHCP parameter extracted from the server's\r
280 response.\r
281**/\r
282VOID\r
283DhcpComputeLease (\r
284 IN OUT DHCP_SERVICE *DhcpSb,\r
285 IN DHCP_PARAMETER *Para\r
286 )\r
287{\r
288 ASSERT (Para != NULL);\r
289\r
290 DhcpSb->Lease = Para->Lease;\r
291 DhcpSb->T2 = Para->T2;\r
292 DhcpSb->T1 = Para->T1;\r
293\r
294 if (DhcpSb->Lease == 0) {\r
295 DhcpSb->Lease = DHCP_DEFAULT_LEASE;\r
296 }\r
297\r
298 if ((DhcpSb->T2 == 0) || (DhcpSb->T2 >= Para->Lease)) {\r
299 DhcpSb->T2 = Para->Lease - (Para->Lease >> 3);\r
300 }\r
301\r
302 if ((DhcpSb->T1 == 0) || (DhcpSb->T1 >= Para->T2)) {\r
303 DhcpSb->T1 = DhcpSb->Lease >> 1;\r
304 }\r
305}\r
306\r
307\r
308/**\r
309 Configure a UDP IO port to use the acquired lease address.\r
310 DHCP driver needs this port to unicast packet to the server\r
311 such as DHCP release.\r
312\r
313 @param[in] UdpIo The UDP IO to configure\r
314 @param[in] Context Dhcp service instance.\r
315\r
316 @retval EFI_SUCCESS The UDP IO port is successfully configured.\r
317 @retval Others It failed to configure the port.\r
318\r
319**/\r
320EFI_STATUS\r
321EFIAPI\r
322DhcpConfigLeaseIoPort (\r
323 IN UDP_IO *UdpIo,\r
324 IN VOID *Context\r
325 )\r
326{\r
327 EFI_UDP4_CONFIG_DATA UdpConfigData;\r
328 EFI_IPv4_ADDRESS Subnet;\r
329 EFI_IPv4_ADDRESS Gateway;\r
330 DHCP_SERVICE *DhcpSb;\r
331 EFI_STATUS Status;\r
332 IP4_ADDR Ip;\r
333\r
334 DhcpSb = (DHCP_SERVICE *) Context;\r
335\r
336 UdpConfigData.AcceptBroadcast = FALSE;\r
337 UdpConfigData.AcceptPromiscuous = FALSE;\r
338 UdpConfigData.AcceptAnyPort = FALSE;\r
339 UdpConfigData.AllowDuplicatePort = TRUE;\r
340 UdpConfigData.TypeOfService = 0;\r
341 UdpConfigData.TimeToLive = 64;\r
342 UdpConfigData.DoNotFragment = FALSE;\r
343 UdpConfigData.ReceiveTimeout = 1;\r
344 UdpConfigData.TransmitTimeout = 0;\r
345\r
346 UdpConfigData.UseDefaultAddress = FALSE;\r
347 UdpConfigData.StationPort = DHCP_CLIENT_PORT;\r
348 UdpConfigData.RemotePort = DHCP_SERVER_PORT;\r
349\r
350 Ip = HTONL (DhcpSb->ClientAddr);\r
351 CopyMem (&UdpConfigData.StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
352\r
353 Ip = HTONL (DhcpSb->Netmask);\r
354 CopyMem (&UdpConfigData.SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
355\r
356 ZeroMem (&UdpConfigData.RemoteAddress, sizeof (EFI_IPv4_ADDRESS));\r
357\r
358 Status = UdpIo->Protocol.Udp4->Configure (UdpIo->Protocol.Udp4, &UdpConfigData);\r
359\r
360 if (EFI_ERROR (Status)) {\r
361 return Status;\r
362 }\r
363\r
364 //\r
365 // Add a default route if received from the server.\r
366 //\r
367 if ((DhcpSb->Para != NULL) && (DhcpSb->Para->Router != 0)) {\r
368 ZeroMem (&Subnet, sizeof (EFI_IPv4_ADDRESS));\r
369\r
370 Ip = HTONL (DhcpSb->Para->Router);\r
371 CopyMem (&Gateway, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
372\r
373 UdpIo->Protocol.Udp4->Routes (UdpIo->Protocol.Udp4, FALSE, &Subnet, &Subnet, &Gateway);\r
374 }\r
375\r
376 return EFI_SUCCESS;\r
377}\r
378\r
379\r
380/**\r
381 Update the lease states when a new lease is acquired. It will not only\r
382 save the acquired the address and lease time, it will also create a UDP\r
383 child to provide address resolution for the address.\r
384\r
385 @param DhcpSb The DHCP service instance\r
386\r
387 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.\r
388 @retval EFI_SUCCESS The lease is recorded.\r
389\r
390**/\r
391EFI_STATUS\r
392DhcpLeaseAcquired (\r
393 IN OUT DHCP_SERVICE *DhcpSb\r
394 )\r
395{\r
396 DhcpSb->ClientAddr = EFI_NTOHL (DhcpSb->Selected->Dhcp4.Header.YourAddr);\r
397\r
398 if (DhcpSb->Para != NULL) {\r
399 DhcpSb->Netmask = DhcpSb->Para->NetMask;\r
400 DhcpSb->ServerAddr = DhcpSb->Para->ServerId;\r
401 }\r
402\r
403 if (DhcpSb->Netmask == 0) {\r
404 return EFI_ABORTED;\r
405 }\r
406\r
407 if (DhcpSb->LeaseIoPort != NULL) {\r
408 UdpIoFreeIo (DhcpSb->LeaseIoPort);\r
409 }\r
410\r
411 //\r
412 // Create a UDP/IP child to provide ARP service for the Leased IP,\r
413 // and transmit unicast packet with it as source address. Don't\r
414 // start receive on this port, the queued packet will be timeout.\r
415 //\r
416 DhcpSb->LeaseIoPort = UdpIoCreateIo (\r
417 DhcpSb->Controller,\r
418 DhcpSb->Image,\r
419 DhcpConfigLeaseIoPort,\r
420 UDP_IO_UDP4_VERSION,\r
421 DhcpSb\r
422 );\r
423\r
424 if (DhcpSb->LeaseIoPort == NULL) {\r
425 return EFI_OUT_OF_RESOURCES;\r
426 }\r
427\r
428 if (!DHCP_IS_BOOTP (DhcpSb->Para)) {\r
429 DhcpComputeLease (DhcpSb, DhcpSb->Para);\r
430 }\r
431\r
432 return DhcpSetState (DhcpSb, Dhcp4Bound, TRUE);\r
433}\r
434\r
435\r
436/**\r
437 Clean up the DHCP related states, IoStatus isn't reset.\r
438\r
439 @param DhcpSb The DHCP instance service.\r
440\r
441**/\r
442VOID\r
443DhcpCleanLease (\r
444 IN DHCP_SERVICE *DhcpSb\r
445 )\r
446{\r
447 DhcpSb->DhcpState = Dhcp4Init;\r
448 DhcpSb->Xid = DhcpSb->Xid + 1;\r
449 DhcpSb->ClientAddr = 0;\r
450 DhcpSb->Netmask = 0;\r
451 DhcpSb->ServerAddr = 0;\r
452\r
453 if (DhcpSb->LastOffer != NULL) {\r
454 FreePool (DhcpSb->LastOffer);\r
455 DhcpSb->LastOffer = NULL;\r
456 }\r
457\r
458 if (DhcpSb->Selected != NULL) {\r
459 FreePool (DhcpSb->Selected);\r
460 DhcpSb->Selected = NULL;\r
461 }\r
462\r
463 if (DhcpSb->Para != NULL) {\r
464 FreePool (DhcpSb->Para);\r
465 DhcpSb->Para = NULL;\r
466 }\r
467\r
468 DhcpSb->Lease = 0;\r
469 DhcpSb->T1 = 0;\r
470 DhcpSb->T2 = 0;\r
471 DhcpSb->ExtraRefresh = FALSE;\r
472\r
473 if (DhcpSb->LeaseIoPort != NULL) {\r
474 UdpIoFreeIo (DhcpSb->LeaseIoPort);\r
475 DhcpSb->LeaseIoPort = NULL;\r
476 }\r
477\r
478 if (DhcpSb->LastPacket != NULL) {\r
479 FreePool (DhcpSb->LastPacket);\r
480 DhcpSb->LastPacket = NULL;\r
481 }\r
482\r
483 DhcpSb->PacketToLive = 0;\r
484 DhcpSb->LastTimeout = 0;\r
485 DhcpSb->CurRetry = 0;\r
486 DhcpSb->MaxRetries = 0;\r
487 DhcpSb->LeaseLife = 0;\r
488\r
489 //\r
490 // Clean active config data.\r
491 //\r
492 DhcpCleanConfigure (&DhcpSb->ActiveConfig);\r
493}\r
494\r
495\r
496/**\r
497 Select a offer among all the offers collected. If the offer selected is\r
498 of BOOTP, the lease is recorded and user notified. If the offer is of\r
499 DHCP, it will request the offer from the server.\r
500\r
501 @param[in] DhcpSb The DHCP service instance.\r
502\r
503 @retval EFI_SUCCESS One of the offer is selected.\r
504\r
505**/\r
506EFI_STATUS\r
507DhcpChooseOffer (\r
508 IN DHCP_SERVICE *DhcpSb\r
509 )\r
510{\r
511 EFI_DHCP4_PACKET *Selected;\r
512 EFI_DHCP4_PACKET *NewPacket;\r
513 EFI_DHCP4_PACKET *TempPacket;\r
514 EFI_STATUS Status;\r
515\r
516 ASSERT (DhcpSb->LastOffer != NULL);\r
517\r
518 //\r
519 // User will cache previous offers if he wants to select\r
520 // from multiple offers. If user provides an invalid packet,\r
521 // use the last offer, otherwise use the provided packet.\r
522 //\r
523 NewPacket = NULL;\r
524 Status = DhcpCallUser (DhcpSb, Dhcp4SelectOffer, DhcpSb->LastOffer, &NewPacket);\r
525\r
526 if (EFI_ERROR (Status)) {\r
527 return Status;\r
528 }\r
529\r
530 Selected = DhcpSb->LastOffer;\r
531\r
532 if ((NewPacket != NULL) && !EFI_ERROR (DhcpValidateOptions (NewPacket, NULL))) {\r
533 TempPacket = (EFI_DHCP4_PACKET *) AllocatePool (NewPacket->Size);\r
534 if (TempPacket != NULL) {\r
535 CopyMem (TempPacket, NewPacket, NewPacket->Size);\r
536 FreePool (Selected);\r
537 Selected = TempPacket;\r
538 }\r
539 }\r
540\r
541 DhcpSb->Selected = Selected;\r
542 DhcpSb->LastOffer = NULL;\r
543 DhcpSb->Para = NULL;\r
544 DhcpValidateOptions (Selected, &DhcpSb->Para);\r
545\r
546 //\r
547 // A bootp offer has been selected, save the lease status,\r
548 // enter bound state then notify the user.\r
549 //\r
550 if (DHCP_IS_BOOTP (DhcpSb->Para)) {\r
551 Status = DhcpLeaseAcquired (DhcpSb);\r
552\r
553 if (EFI_ERROR (Status)) {\r
554 return Status;\r
555 }\r
556\r
557 DhcpSb->IoStatus = EFI_SUCCESS;\r
558 DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_ALL);\r
559 return EFI_SUCCESS;\r
560 }\r
561\r
562 //\r
563 // Send a DHCP requests\r
564 //\r
565 Status = DhcpSetState (DhcpSb, Dhcp4Requesting, TRUE);\r
566\r
567 if (EFI_ERROR (Status)) {\r
568 return Status;\r
569 }\r
570\r
571 return DhcpSendMessage (DhcpSb, Selected, DhcpSb->Para, DHCP_MSG_REQUEST, NULL);\r
572}\r
573\r
574\r
575/**\r
576 Terminate the current address acquire. All the allocated resources\r
577 are released. Be careful when calling this function. A rule related\r
578 to this is: only call DhcpEndSession at the highest level, such as\r
579 DhcpInput, DhcpOnTimerTick...At the other level, just return error.\r
580\r
581 @param[in] DhcpSb The DHCP service instance\r
582 @param[in] Status The result of the DHCP process.\r
583\r
584**/\r
585VOID\r
586DhcpEndSession (\r
587 IN DHCP_SERVICE *DhcpSb,\r
588 IN EFI_STATUS Status\r
589 )\r
590{\r
591 if (DHCP_CONNECTED (DhcpSb->DhcpState)) {\r
592 DhcpCallUser (DhcpSb, Dhcp4AddressLost, NULL, NULL);\r
593 } else {\r
594 DhcpCallUser (DhcpSb, Dhcp4Fail, NULL, NULL);\r
595 }\r
596\r
597 DhcpCleanLease (DhcpSb);\r
598\r
599 DhcpSb->IoStatus = Status;\r
600 DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_ALL);\r
601}\r
602\r
603\r
604/**\r
605 Handle packets in DHCP select state.\r
606\r
607 @param[in] DhcpSb The DHCP service instance\r
608 @param[in] Packet The DHCP packet received\r
609 @param[in] Para The DHCP parameter extracted from the packet. That\r
610 is, all the option value that we care.\r
611\r
612 @retval EFI_SUCCESS The packet is successfully processed.\r
613 @retval Others Some error occured.\r
614\r
615**/\r
616EFI_STATUS\r
617DhcpHandleSelect (\r
618 IN DHCP_SERVICE *DhcpSb,\r
619 IN EFI_DHCP4_PACKET *Packet,\r
620 IN DHCP_PARAMETER *Para\r
621 )\r
622{\r
623 EFI_STATUS Status;\r
624\r
625 Status = EFI_SUCCESS;\r
626\r
627 //\r
628 // First validate the message:\r
629 // 1. the offer is a unicast\r
630 // 2. if it is a DHCP message, it must contains a server ID.\r
631 // Don't return a error for these two case otherwise the session is ended.\r
632 //\r
633 if (!DHCP_IS_BOOTP (Para) &&\r
634 ((Para->DhcpType != DHCP_MSG_OFFER) || (Para->ServerId == 0))\r
635 ) {\r
636 goto ON_EXIT;\r
637 }\r
638\r
639 //\r
640 // Call the user's callback. The action according to the return is as:\r
641 // 1. EFI_SUCESS: stop waiting for more offers, select the offer now\r
642 // 2. EFI_NOT_READY: wait for more offers\r
643 // 3. EFI_ABORTED: abort the address acquiring.\r
644 //\r
645 Status = DhcpCallUser (DhcpSb, Dhcp4RcvdOffer, Packet, NULL);\r
646\r
647 if (Status == EFI_SUCCESS) {\r
648 if (DhcpSb->LastOffer != NULL) {\r
649 FreePool (DhcpSb->LastOffer);\r
650 }\r
651\r
652 DhcpSb->LastOffer = Packet;\r
653\r
654 return DhcpChooseOffer (DhcpSb);\r
655\r
656 } else if (Status == EFI_NOT_READY) {\r
657 if (DhcpSb->LastOffer != NULL) {\r
658 FreePool (DhcpSb->LastOffer);\r
659 }\r
660\r
661 DhcpSb->LastOffer = Packet;\r
662\r
663 } else if (Status == EFI_ABORTED) {\r
664 //\r
665 // DhcpInput will end the session upon error return. Remember\r
666 // only to call DhcpEndSession at the top level call.\r
667 //\r
668 goto ON_EXIT;\r
669 }\r
670\r
671 return EFI_SUCCESS;\r
672\r
673ON_EXIT:\r
674 FreePool (Packet);\r
675 return Status;\r
676}\r
677\r
678\r
679/**\r
680 Handle packets in DHCP request state.\r
681\r
682 @param[in] DhcpSb The DHCP service instance\r
683 @param[in] Packet The DHCP packet received\r
684 @param[in] Para The DHCP parameter extracted from the packet. That\r
685 is, all the option value that we care.\r
686\r
687 @retval EFI_SUCCESS The packet is successfully processed.\r
688 @retval Others Some error occured.\r
689\r
690**/\r
691EFI_STATUS\r
692DhcpHandleRequest (\r
693 IN DHCP_SERVICE *DhcpSb,\r
694 IN EFI_DHCP4_PACKET *Packet,\r
695 IN DHCP_PARAMETER *Para\r
696 )\r
697{\r
698 EFI_DHCP4_HEADER *Head;\r
699 EFI_DHCP4_HEADER *Selected;\r
700 EFI_STATUS Status;\r
701 UINT8 *Message;\r
702\r
703 ASSERT (!DHCP_IS_BOOTP (DhcpSb->Para));\r
704\r
705 Head = &Packet->Dhcp4.Header;\r
706 Selected = &DhcpSb->Selected->Dhcp4.Header;\r
707\r
708 //\r
709 // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK.\r
710 //\r
711 if (DHCP_IS_BOOTP (Para) ||\r
712 (Para->ServerId != DhcpSb->Para->ServerId) ||\r
713 ((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK))\r
714 ) {\r
715\r
716 Status = EFI_SUCCESS;\r
717 goto ON_EXIT;\r
718 }\r
719\r
720 //\r
721 // Received a NAK, end the session no matter what the user returns\r
722 //\r
723 Status = EFI_DEVICE_ERROR;\r
724\r
725 if (Para->DhcpType == DHCP_MSG_NAK) {\r
726 DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL);\r
727 goto ON_EXIT;\r
728 }\r
729\r
730 //\r
731 // Check whether the ACK matches the selected offer\r
732 //\r
733 Message = NULL;\r
734\r
735 if (!EFI_IP4_EQUAL (&Head->YourAddr, &Selected->YourAddr)) {\r
736 Message = (UINT8 *) "Lease confirmed isn't the same as that in the offer";\r
737 goto REJECT;\r
738 }\r
739\r
740 Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL);\r
741\r
742 if (EFI_ERROR (Status)) {\r
743 Message = (UINT8 *) "Lease is denied upon received ACK";\r
744 goto REJECT;\r
745 }\r
746\r
747 //\r
748 // Record the lease, transit to BOUND state, then notify the user\r
749 //\r
750 Status = DhcpLeaseAcquired (DhcpSb);\r
751\r
752 if (EFI_ERROR (Status)) {\r
753 Message = (UINT8 *) "Lease is denied upon entering bound";\r
754 goto REJECT;\r
755 }\r
756\r
757 DhcpSb->IoStatus = EFI_SUCCESS;\r
758 DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_COMPLETION);\r
759\r
760 FreePool (Packet);\r
761 return EFI_SUCCESS;\r
762\r
763REJECT:\r
764 DhcpSendMessage (DhcpSb, DhcpSb->Selected, DhcpSb->Para, DHCP_MSG_DECLINE, Message);\r
765\r
766ON_EXIT:\r
767 FreePool (Packet);\r
768 return Status;\r
769}\r
770\r
771\r
772/**\r
773 Handle packets in DHCP renew/rebound state.\r
774\r
775 @param[in] DhcpSb The DHCP service instance\r
776 @param[in] Packet The DHCP packet received\r
777 @param[in] Para The DHCP parameter extracted from the packet. That\r
778 is, all the option value that we care.\r
779\r
780 @retval EFI_SUCCESS The packet is successfully processed.\r
781 @retval Others Some error occured.\r
782\r
783**/\r
784EFI_STATUS\r
785DhcpHandleRenewRebind (\r
786 IN DHCP_SERVICE *DhcpSb,\r
787 IN EFI_DHCP4_PACKET *Packet,\r
788 IN DHCP_PARAMETER *Para\r
789 )\r
790{\r
791 EFI_DHCP4_HEADER *Head;\r
792 EFI_DHCP4_HEADER *Selected;\r
793 EFI_STATUS Status;\r
794\r
795 ASSERT (!DHCP_IS_BOOTP (DhcpSb->Para));\r
796\r
797 Head = &Packet->Dhcp4.Header;\r
798 Selected = &DhcpSb->Selected->Dhcp4.Header;\r
799\r
800 //\r
801 // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK\r
802 //\r
803 if (DHCP_IS_BOOTP (Para) ||\r
804 (Para->ServerId != DhcpSb->Para->ServerId) ||\r
805 ((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK))\r
806 ) {\r
807\r
808 Status = EFI_SUCCESS;\r
809 goto ON_EXIT;\r
810 }\r
811\r
812 //\r
813 // Received a NAK, ignore the user's return then terminate the process\r
814 //\r
815 Status = EFI_DEVICE_ERROR;\r
816\r
817 if (Para->DhcpType == DHCP_MSG_NAK) {\r
818 DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL);\r
819 goto ON_EXIT;\r
820 }\r
821\r
822 //\r
823 // The lease is different from the selected. Don't send a DECLINE\r
824 // since it isn't existed in the client's FSM.\r
825 //\r
826 if (!EFI_IP4_EQUAL (&Head->YourAddr, &Selected->YourAddr)) {\r
827 goto ON_EXIT;\r
828 }\r
829\r
830 Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL);\r
831\r
832 if (EFI_ERROR (Status)) {\r
833 goto ON_EXIT;\r
834 }\r
835\r
836 //\r
837 // Record the lease, start timer for T1 and T2,\r
838 //\r
839 DhcpComputeLease (DhcpSb, Para);\r
840 DhcpSb->LeaseLife = 0;\r
841 DhcpSetState (DhcpSb, Dhcp4Bound, TRUE);\r
842\r
843 if (DhcpSb->ExtraRefresh != 0) {\r
844 DhcpSb->ExtraRefresh = FALSE;\r
845\r
846 DhcpSb->IoStatus = EFI_SUCCESS;\r
847 DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_RENEWREBIND);\r
848 }\r
849\r
850ON_EXIT:\r
851 FreePool (Packet);\r
852 return Status;\r
853}\r
854\r
855\r
856/**\r
857 Handle packets in DHCP reboot state.\r
858\r
859 @param[in] DhcpSb The DHCP service instance\r
860 @param[in] Packet The DHCP packet received\r
861 @param[in] Para The DHCP parameter extracted from the packet. That\r
862 is, all the option value that we care.\r
863\r
864 @retval EFI_SUCCESS The packet is successfully processed.\r
865 @retval Others Some error occured.\r
866\r
867**/\r
868EFI_STATUS\r
869DhcpHandleReboot (\r
870 IN DHCP_SERVICE *DhcpSb,\r
871 IN EFI_DHCP4_PACKET *Packet,\r
872 IN DHCP_PARAMETER *Para\r
873 )\r
874{\r
875 EFI_DHCP4_HEADER *Head;\r
876 EFI_STATUS Status;\r
877\r
878 Head = &Packet->Dhcp4.Header;\r
879\r
880 //\r
881 // Ignore the BOOTP message and DHCP messages other than DHCP ACK/NACK\r
882 //\r
883 if (DHCP_IS_BOOTP (Para) ||\r
884 ((Para->DhcpType != DHCP_MSG_ACK) && (Para->DhcpType != DHCP_MSG_NAK))\r
885 ) {\r
886\r
887 Status = EFI_SUCCESS;\r
888 goto ON_EXIT;\r
889 }\r
890\r
891 //\r
892 // If a NAK is received, transit to INIT and try again.\r
893 //\r
894 if (Para->DhcpType == DHCP_MSG_NAK) {\r
895 DhcpCallUser (DhcpSb, Dhcp4RcvdNak, Packet, NULL);\r
896\r
897 DhcpSb->ClientAddr = 0;\r
898 DhcpSb->DhcpState = Dhcp4Init;\r
899\r
900 Status = DhcpInitRequest (DhcpSb);\r
901 goto ON_EXIT;\r
902 }\r
903\r
904 //\r
905 // Check whether the ACK matches the selected offer\r
906 //\r
907 if (EFI_NTOHL (Head->YourAddr) != DhcpSb->ClientAddr) {\r
908 Status = EFI_DEVICE_ERROR;\r
909 goto ON_EXIT;\r
910 }\r
911\r
912 Status = DhcpCallUser (DhcpSb, Dhcp4RcvdAck, Packet, NULL);\r
913 if (EFI_ERROR (Status)) {\r
914 goto ON_EXIT;\r
915 }\r
916\r
917 //\r
918 // OK, get the parameter from server, record the lease\r
919 //\r
920 DhcpSb->Para = AllocateCopyPool (sizeof (DHCP_PARAMETER), Para);\r
921 if (DhcpSb->Para == NULL) {\r
922 Status = EFI_OUT_OF_RESOURCES;\r
923 goto ON_EXIT;\r
924 }\r
925\r
926 DhcpSb->Selected = Packet;\r
927 Status = DhcpLeaseAcquired (DhcpSb);\r
928 if (EFI_ERROR (Status)) {\r
929 return Status;\r
930 }\r
931\r
932 DhcpSb->IoStatus = EFI_SUCCESS;\r
933 DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_COMPLETION);\r
934 return EFI_SUCCESS;\r
935\r
936ON_EXIT:\r
937 FreePool (Packet);\r
938 return Status;\r
939}\r
940\r
941\r
942/**\r
943 Handle the received DHCP packets. This function drives the DHCP\r
944 state machine.\r
945\r
946 @param UdpPacket The UDP packets received.\r
947 @param EndPoint The local/remote UDP access point\r
948 @param IoStatus The status of the UDP receive\r
949 @param Context The opaque parameter to the function.\r
950\r
951**/\r
952VOID\r
953EFIAPI\r
954DhcpInput (\r
955 NET_BUF *UdpPacket,\r
956 UDP_END_POINT *EndPoint,\r
957 EFI_STATUS IoStatus,\r
958 VOID *Context\r
959 )\r
960{\r
961 DHCP_SERVICE *DhcpSb;\r
962 EFI_DHCP4_HEADER *Head;\r
963 EFI_DHCP4_PACKET *Packet;\r
964 DHCP_PARAMETER *Para;\r
965 EFI_STATUS Status;\r
966 UINT32 Len;\r
967\r
968 Packet = NULL;\r
969 DhcpSb = (DHCP_SERVICE *) Context;\r
970\r
971 //\r
972 // Don't restart receive if error occurs or DHCP is destroyed.\r
973 //\r
974 if (EFI_ERROR (IoStatus)) {\r
975 return ;\r
976 } else if (DhcpSb->ServiceState == DHCP_DESTROY) {\r
977 NetbufFree (UdpPacket);\r
978 return ;\r
979 }\r
980\r
981 ASSERT (UdpPacket != NULL);\r
982\r
983 if (DhcpSb->DhcpState == Dhcp4Stopped) {\r
984 goto RESTART;\r
985 }\r
986\r
987 //\r
988 // Validate the packet received\r
989 //\r
990 if (UdpPacket->TotalSize < sizeof (EFI_DHCP4_HEADER)) {\r
991 goto RESTART;\r
992 }\r
993\r
994 //\r
995 // Copy the DHCP message to a continuous memory block\r
996 //\r
997 Len = sizeof (EFI_DHCP4_PACKET) + UdpPacket->TotalSize - sizeof (EFI_DHCP4_HEADER);\r
998 Packet = (EFI_DHCP4_PACKET *) AllocatePool (Len);\r
999\r
1000 if (Packet == NULL) {\r
1001 goto RESTART;\r
1002 }\r
1003\r
1004 Packet->Size = Len;\r
1005 Head = &Packet->Dhcp4.Header;\r
1006 Packet->Length = NetbufCopy (UdpPacket, 0, UdpPacket->TotalSize, (UINT8 *) Head);\r
1007\r
1008 if (Packet->Length != UdpPacket->TotalSize) {\r
1009 goto RESTART;\r
1010 }\r
1011\r
1012 //\r
1013 // Is this packet the answer to our packet?\r
1014 //\r
1015 if ((Head->OpCode != BOOTP_REPLY) ||\r
1016 (NTOHL (Head->Xid) != DhcpSb->Xid) ||\r
1017 (CompareMem (DhcpSb->ClientAddressSendOut, Head->ClientHwAddr, Head->HwAddrLen) != 0)) {\r
1018 goto RESTART;\r
1019 }\r
1020\r
1021 //\r
1022 // Validate the options and retrieve the interested options\r
1023 //\r
1024 Para = NULL;\r
1025 if ((Packet->Length > sizeof (EFI_DHCP4_HEADER) + sizeof (UINT32)) &&\r
1026 (Packet->Dhcp4.Magik == DHCP_OPTION_MAGIC) &&\r
1027 EFI_ERROR (DhcpValidateOptions (Packet, &Para))) {\r
1028\r
1029 goto RESTART;\r
1030 }\r
1031\r
1032 //\r
1033 // Call the handler for each state. The handler should return\r
1034 // EFI_SUCCESS if the process can go on no matter whether the\r
1035 // packet is ignored or not. If the return is EFI_ERROR, the\r
1036 // session will be terminated. Packet's ownership is handled\r
1037 // over to the handlers. If operation succeeds, the handler\r
1038 // must notify the user. It isn't necessary to do if EFI_ERROR\r
1039 // is returned because the DhcpEndSession will notify the user.\r
1040 //\r
1041 Status = EFI_SUCCESS;\r
1042\r
1043 switch (DhcpSb->DhcpState) {\r
1044 case Dhcp4Selecting:\r
1045 Status = DhcpHandleSelect (DhcpSb, Packet, Para);\r
1046 break;\r
1047\r
1048 case Dhcp4Requesting:\r
1049 Status = DhcpHandleRequest (DhcpSb, Packet, Para);\r
1050 break;\r
1051\r
1052 case Dhcp4InitReboot:\r
1053 case Dhcp4Init:\r
1054 case Dhcp4Bound:\r
1055 //\r
1056 // Ignore the packet in INITREBOOT, INIT and BOUND states\r
1057 //\r
1058 FreePool (Packet);\r
1059 Status = EFI_SUCCESS;\r
1060 break;\r
1061\r
1062 case Dhcp4Renewing:\r
1063 case Dhcp4Rebinding:\r
1064 Status = DhcpHandleRenewRebind (DhcpSb, Packet, Para);\r
1065 break;\r
1066\r
1067 case Dhcp4Rebooting:\r
1068 Status = DhcpHandleReboot (DhcpSb, Packet, Para);\r
1069 break;\r
1070 }\r
1071\r
1072 if (Para != NULL) {\r
1073 FreePool (Para);\r
1074 }\r
1075\r
1076 Packet = NULL;\r
1077\r
1078 if (EFI_ERROR (Status)) {\r
1079 NetbufFree (UdpPacket);\r
1080 UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0);\r
1081 DhcpEndSession (DhcpSb, Status);\r
1082 return ;\r
1083 }\r
1084\r
1085RESTART:\r
1086 NetbufFree (UdpPacket);\r
1087\r
1088 if (Packet != NULL) {\r
1089 FreePool (Packet);\r
1090 }\r
1091\r
1092 Status = UdpIoRecvDatagram (DhcpSb->UdpIo, DhcpInput, DhcpSb, 0);\r
1093\r
1094 if (EFI_ERROR (Status)) {\r
1095 DhcpEndSession (DhcpSb, EFI_DEVICE_ERROR);\r
1096 }\r
1097}\r
1098\r
1099\r
1100/**\r
1101 Release the packet.\r
1102\r
1103 @param[in] Arg The packet to release\r
1104\r
1105**/\r
1106VOID\r
1107EFIAPI\r
1108DhcpReleasePacket (\r
1109 IN VOID *Arg\r
1110 )\r
1111{\r
1112 FreePool (Arg);\r
1113}\r
1114\r
1115\r
1116/**\r
1117 Release the net buffer when packet is sent.\r
1118\r
1119 @param UdpPacket The UDP packets received.\r
1120 @param EndPoint The local/remote UDP access point\r
1121 @param IoStatus The status of the UDP receive\r
1122 @param Context The opaque parameter to the function.\r
1123\r
1124**/\r
1125VOID\r
1126EFIAPI\r
1127DhcpOnPacketSent (\r
1128 NET_BUF *Packet,\r
1129 UDP_END_POINT *EndPoint,\r
1130 EFI_STATUS IoStatus,\r
1131 VOID *Context\r
1132 )\r
1133{\r
1134 NetbufFree (Packet);\r
1135}\r
1136\r
1137\r
1138\r
1139/**\r
1140 Build and transmit a DHCP message according to the current states.\r
1141 This function implement the Table 5. of RFC 2131. Always transits\r
1142 the state (as defined in Figure 5. of the same RFC) before sending\r
1143 a DHCP message. The table is adjusted accordingly.\r
1144\r
1145 @param[in] DhcpSb The DHCP service instance\r
1146 @param[in] Seed The seed packet which the new packet is based on\r
1147 @param[in] Para The DHCP parameter of the Seed packet\r
1148 @param[in] Type The message type to send\r
1149 @param[in] Msg The human readable message to include in the packet\r
1150 sent.\r
1151\r
1152 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources for the packet\r
1153 @retval EFI_ACCESS_DENIED Failed to transmit the packet through UDP\r
1154 @retval EFI_SUCCESS The message is sent\r
1155 @retval other Other error occurs\r
1156\r
1157**/\r
1158EFI_STATUS\r
1159DhcpSendMessage (\r
1160 IN DHCP_SERVICE *DhcpSb,\r
1161 IN EFI_DHCP4_PACKET *Seed,\r
1162 IN DHCP_PARAMETER *Para,\r
1163 IN UINT8 Type,\r
1164 IN UINT8 *Msg\r
1165 )\r
1166{\r
1167 EFI_DHCP4_CONFIG_DATA *Config;\r
1168 EFI_DHCP4_PACKET *Packet;\r
1169 EFI_DHCP4_PACKET *NewPacket;\r
1170 EFI_DHCP4_HEADER *Head;\r
1171 EFI_DHCP4_HEADER *SeedHead;\r
1172 UDP_IO *UdpIo;\r
1173 UDP_END_POINT EndPoint;\r
1174 NET_BUF *Wrap;\r
1175 NET_FRAGMENT Frag;\r
1176 EFI_STATUS Status;\r
1177 IP4_ADDR IpAddr;\r
1178 UINT8 *Buf;\r
1179 UINT16 MaxMsg;\r
1180 UINT32 Len;\r
1181 UINT32 Index;\r
1182\r
1183 //\r
1184 // Allocate a big enough memory block to hold the DHCP packet\r
1185 //\r
1186 Len = sizeof (EFI_DHCP4_PACKET) + 128 + DhcpSb->UserOptionLen;\r
1187\r
1188 if (Msg != NULL) {\r
1189 Len += (UINT32)AsciiStrLen ((CHAR8 *) Msg);\r
1190 }\r
1191\r
1192 Packet = AllocatePool (Len);\r
1193\r
1194 if (Packet == NULL) {\r
1195 return EFI_OUT_OF_RESOURCES;\r
1196 }\r
1197\r
1198 Packet->Size = Len;\r
1199 Packet->Length = sizeof (EFI_DHCP4_HEADER) + sizeof (UINT32);\r
1200\r
1201 //\r
1202 // Fill in the DHCP header fields\r
1203 //\r
1204 Config = &DhcpSb->ActiveConfig;\r
1205 SeedHead = NULL;\r
1206\r
1207 if (Seed != NULL) {\r
1208 SeedHead = &Seed->Dhcp4.Header;\r
1209 }\r
1210\r
1211 Head = &Packet->Dhcp4.Header;\r
1212 ZeroMem (Head, sizeof (EFI_DHCP4_HEADER));\r
1213\r
1214 Head->OpCode = BOOTP_REQUEST;\r
1215 Head->HwType = DhcpSb->HwType;\r
1216 Head->HwAddrLen = DhcpSb->HwLen;\r
1217 Head->Xid = HTONL (DhcpSb->Xid);\r
1218 Head->Reserved = HTONS (0x8000); //Server, broadcast the message please.\r
1219\r
1220 EFI_IP4 (Head->ClientAddr) = HTONL (DhcpSb->ClientAddr);\r
1221 CopyMem (Head->ClientHwAddr, DhcpSb->Mac.Addr, DhcpSb->HwLen);\r
1222\r
1223 if ((Type == DHCP_MSG_DECLINE) || (Type == DHCP_MSG_RELEASE)) {\r
1224 Head->Seconds = 0;\r
1225 } else if ((Type == DHCP_MSG_REQUEST) && (DhcpSb->DhcpState == Dhcp4Requesting)) {\r
1226 //\r
1227 // Use the same value as the original DHCPDISCOVER message.\r
1228 //\r
1229 Head->Seconds = DhcpSb->LastPacket->Dhcp4.Header.Seconds;\r
1230 } else {\r
1231 SetElapsedTime(&Head->Seconds, DhcpSb->ActiveChild);\r
1232 }\r
1233\r
1234 //\r
1235 // Append the DHCP message type\r
1236 //\r
1237 Packet->Dhcp4.Magik = DHCP_OPTION_MAGIC;\r
1238 Buf = Packet->Dhcp4.Option;\r
1239 Buf = DhcpAppendOption (Buf, DHCP4_TAG_MSG_TYPE, 1, &Type);\r
1240\r
1241 //\r
1242 // Append the serverid option if necessary:\r
1243 // 1. DHCP decline message\r
1244 // 2. DHCP release message\r
1245 // 3. DHCP request to confirm one lease.\r
1246 //\r
1247 if ((Type == DHCP_MSG_DECLINE) || (Type == DHCP_MSG_RELEASE) ||\r
1248 ((Type == DHCP_MSG_REQUEST) && (DhcpSb->DhcpState == Dhcp4Requesting))\r
1249 ) {\r
1250\r
1251 ASSERT ((Para != NULL) && (Para->ServerId != 0));\r
1252\r
1253 IpAddr = HTONL (Para->ServerId);\r
1254 Buf = DhcpAppendOption (Buf, DHCP4_TAG_SERVER_ID, 4, (UINT8 *) &IpAddr);\r
1255 }\r
1256\r
1257 //\r
1258 // Append the requested IP option if necessary:\r
1259 // 1. DHCP request to use the previously allocated address\r
1260 // 2. DHCP request to confirm one lease\r
1261 // 3. DHCP decline to decline one lease\r
1262 //\r
1263 IpAddr = 0;\r
1264\r
1265 if (Type == DHCP_MSG_REQUEST) {\r
1266 if (DhcpSb->DhcpState == Dhcp4Rebooting) {\r
1267 IpAddr = EFI_IP4 (Config->ClientAddress);\r
1268\r
1269 } else if (DhcpSb->DhcpState == Dhcp4Requesting) {\r
1270 ASSERT (SeedHead != NULL);\r
1271 IpAddr = EFI_IP4 (SeedHead->YourAddr);\r
1272 }\r
1273\r
1274 } else if (Type == DHCP_MSG_DECLINE) {\r
1275 ASSERT (SeedHead != NULL);\r
1276 IpAddr = EFI_IP4 (SeedHead->YourAddr);\r
1277 }\r
1278\r
1279 if (IpAddr != 0) {\r
1280 Buf = DhcpAppendOption (Buf, DHCP4_TAG_REQUEST_IP, 4, (UINT8 *) &IpAddr);\r
1281 }\r
1282\r
1283 //\r
1284 // Append the Max Message Length option if it isn't a DECLINE\r
1285 // or RELEASE to direct the server use large messages instead of\r
1286 // override the BOOTFILE and SERVER fields in the message head.\r
1287 //\r
1288 if ((Type != DHCP_MSG_DECLINE) && (Type != DHCP_MSG_RELEASE)) {\r
1289 MaxMsg = HTONS (0xFF00);\r
1290 Buf = DhcpAppendOption (Buf, DHCP4_TAG_MAXMSG, 2, (UINT8 *) &MaxMsg);\r
1291 }\r
1292\r
1293 //\r
1294 // Append the user's message if it isn't NULL\r
1295 //\r
1296 if (Msg != NULL) {\r
1297 Len = MIN ((UINT32) AsciiStrLen ((CHAR8 *) Msg), 255);\r
1298 Buf = DhcpAppendOption (Buf, DHCP4_TAG_MESSAGE, (UINT16) Len, Msg);\r
1299 }\r
1300\r
1301 //\r
1302 // Append the user configured options\r
1303 //\r
1304 if (DhcpSb->UserOptionLen != 0) {\r
1305 for (Index = 0; Index < Config->OptionCount; Index++) {\r
1306 //\r
1307 // We can't use any option other than the client ID from user\r
1308 // if it is a DHCP decline or DHCP release .\r
1309 //\r
1310 if (((Type == DHCP_MSG_DECLINE) || (Type == DHCP_MSG_RELEASE)) &&\r
1311 (Config->OptionList[Index]->OpCode != DHCP4_TAG_CLIENT_ID)) {\r
1312 continue;\r
1313 }\r
1314\r
1315 Buf = DhcpAppendOption (\r
1316 Buf,\r
1317 Config->OptionList[Index]->OpCode,\r
1318 Config->OptionList[Index]->Length,\r
1319 Config->OptionList[Index]->Data\r
1320 );\r
1321 }\r
1322 }\r
1323\r
1324 *(Buf++) = DHCP4_TAG_EOP;\r
1325 Packet->Length += (UINT32) (Buf - Packet->Dhcp4.Option);\r
1326\r
1327 //\r
1328 // OK, the message is built, call the user to override it.\r
1329 //\r
1330 Status = EFI_SUCCESS;\r
1331 NewPacket = NULL;\r
1332\r
1333 if (Type == DHCP_MSG_DISCOVER) {\r
1334 Status = DhcpCallUser (DhcpSb, Dhcp4SendDiscover, Packet, &NewPacket);\r
1335\r
1336 } else if (Type == DHCP_MSG_REQUEST) {\r
1337 Status = DhcpCallUser (DhcpSb, Dhcp4SendRequest, Packet, &NewPacket);\r
1338\r
1339 } else if (Type == DHCP_MSG_DECLINE) {\r
1340 Status = DhcpCallUser (DhcpSb, Dhcp4SendDecline, Packet, &NewPacket);\r
1341 }\r
1342\r
1343 if (EFI_ERROR (Status)) {\r
1344 FreePool (Packet);\r
1345 return Status;\r
1346 }\r
1347\r
1348 if (NewPacket != NULL) {\r
1349 FreePool (Packet);\r
1350 Packet = NewPacket;\r
1351 }\r
1352\r
1353 //\r
1354 // Save the Client Address will be sent out\r
1355 //\r
1356 CopyMem (\r
1357 &DhcpSb->ClientAddressSendOut[0],\r
1358 &Packet->Dhcp4.Header.ClientHwAddr[0],\r
1359 Packet->Dhcp4.Header.HwAddrLen\r
1360 );\r
1361\r
1362\r
1363 //\r
1364 // Wrap it into a netbuf then send it.\r
1365 //\r
1366 Frag.Bulk = (UINT8 *) &Packet->Dhcp4.Header;\r
1367 Frag.Len = Packet->Length;\r
1368 Wrap = NetbufFromExt (&Frag, 1, 0, 0, DhcpReleasePacket, Packet);\r
1369\r
1370 if (Wrap == NULL) {\r
1371 FreePool (Packet);\r
1372 return EFI_OUT_OF_RESOURCES;\r
1373 }\r
1374\r
1375 //\r
1376 // Save it as the last sent packet for retransmission\r
1377 //\r
1378 if (DhcpSb->LastPacket != NULL) {\r
1379 FreePool (DhcpSb->LastPacket);\r
1380 }\r
1381\r
1382 DhcpSb->LastPacket = Packet;\r
1383 DhcpSetTransmitTimer (DhcpSb);\r
1384\r
1385 //\r
1386 // Broadcast the message, unless we know the server address.\r
1387 // Use the lease UdpIo port to send the unicast packet.\r
1388 //\r
1389 EndPoint.RemoteAddr.Addr[0] = 0xffffffff;\r
1390 EndPoint.LocalAddr.Addr[0] = 0;\r
1391 EndPoint.RemotePort = DHCP_SERVER_PORT;\r
1392 EndPoint.LocalPort = DHCP_CLIENT_PORT;\r
1393 UdpIo = DhcpSb->UdpIo;\r
1394\r
1395 if ((DhcpSb->DhcpState == Dhcp4Renewing) || (Type == DHCP_MSG_RELEASE)) {\r
1396 EndPoint.RemoteAddr.Addr[0] = DhcpSb->ServerAddr;\r
1397 EndPoint.LocalAddr.Addr[0] = DhcpSb->ClientAddr;\r
1398 UdpIo = DhcpSb->LeaseIoPort;\r
1399 }\r
1400\r
1401 ASSERT (UdpIo != NULL);\r
1402 NET_GET_REF (Wrap);\r
1403 \r
1404 Status = UdpIoSendDatagram (\r
1405 UdpIo, \r
1406 Wrap, \r
1407 &EndPoint, \r
1408 NULL, \r
1409 DhcpOnPacketSent, \r
1410 DhcpSb\r
1411 );\r
1412\r
1413 if (EFI_ERROR (Status)) {\r
1414 NET_PUT_REF (Wrap);\r
1415 return EFI_ACCESS_DENIED;\r
1416 }\r
1417\r
1418 return EFI_SUCCESS;\r
1419}\r
1420\r
1421\r
1422/**\r
1423 Retransmit a saved packet. Only DISCOVER and REQUEST messages\r
1424 will be retransmitted.\r
1425\r
1426 @param[in] DhcpSb The DHCP service instance\r
1427\r
1428 @retval EFI_ACCESS_DENIED Failed to transmit packet through UDP port\r
1429 @retval EFI_SUCCESS The packet is retransmitted.\r
1430\r
1431**/\r
1432EFI_STATUS\r
1433DhcpRetransmit (\r
1434 IN DHCP_SERVICE *DhcpSb\r
1435 )\r
1436{\r
1437 UDP_IO *UdpIo;\r
1438 UDP_END_POINT EndPoint;\r
1439 NET_BUF *Wrap;\r
1440 NET_FRAGMENT Frag;\r
1441 EFI_STATUS Status;\r
1442\r
1443 ASSERT (DhcpSb->LastPacket != NULL);\r
1444\r
1445 //\r
1446 // For REQUEST message in Dhcp4Requesting state, do not change the secs fields.\r
1447 //\r
1448 if (DhcpSb->DhcpState != Dhcp4Requesting) {\r
1449 SetElapsedTime(&DhcpSb->LastPacket->Dhcp4.Header.Seconds, DhcpSb->ActiveChild);\r
1450 }\r
1451\r
1452 //\r
1453 // Wrap it into a netbuf then send it.\r
1454 //\r
1455 Frag.Bulk = (UINT8 *) &DhcpSb->LastPacket->Dhcp4.Header;\r
1456 Frag.Len = DhcpSb->LastPacket->Length;\r
1457 Wrap = NetbufFromExt (&Frag, 1, 0, 0, DhcpReleasePacket, DhcpSb->LastPacket);\r
1458\r
1459 if (Wrap == NULL) {\r
1460 return EFI_OUT_OF_RESOURCES;\r
1461 }\r
1462 \r
1463 //\r
1464 // Broadcast the message, unless we know the server address.\r
1465 //\r
1466 EndPoint.RemotePort = DHCP_SERVER_PORT;\r
1467 EndPoint.LocalPort = DHCP_CLIENT_PORT;\r
1468 EndPoint.RemoteAddr.Addr[0] = 0xffffffff;\r
1469 EndPoint.LocalAddr.Addr[0] = 0;\r
1470 UdpIo = DhcpSb->UdpIo;\r
1471\r
1472 if (DhcpSb->DhcpState == Dhcp4Renewing) {\r
1473 EndPoint.RemoteAddr.Addr[0] = DhcpSb->ServerAddr;\r
1474 EndPoint.LocalAddr.Addr[0] = DhcpSb->ClientAddr;\r
1475 UdpIo = DhcpSb->LeaseIoPort;\r
1476 }\r
1477\r
1478 ASSERT (UdpIo != NULL);\r
1479\r
1480 NET_GET_REF (Wrap);\r
1481 Status = UdpIoSendDatagram (\r
1482 UdpIo,\r
1483 Wrap,\r
1484 &EndPoint,\r
1485 NULL,\r
1486 DhcpOnPacketSent,\r
1487 DhcpSb\r
1488 );\r
1489\r
1490 if (EFI_ERROR (Status)) {\r
1491 NET_PUT_REF (Wrap);\r
1492 return EFI_ACCESS_DENIED;\r
1493 }\r
1494\r
1495 return EFI_SUCCESS;\r
1496}\r
1497\r
1498\r
1499/**\r
1500 Each DHCP service has three timer. Two of them are count down timer.\r
1501 One for the packet retransmission. The other is to collect the offers.\r
1502 The third timer increaments the lease life which is compared to T1, T2,\r
1503 and lease to determine the time to renew and rebind the lease.\r
1504 DhcpOnTimerTick will be called once every second.\r
1505\r
1506 @param[in] Event The timer event\r
1507 @param[in] Context The context, which is the DHCP service instance.\r
1508\r
1509**/\r
1510VOID\r
1511EFIAPI\r
1512DhcpOnTimerTick (\r
1513 IN EFI_EVENT Event,\r
1514 IN VOID *Context\r
1515 )\r
1516{\r
1517 LIST_ENTRY *Entry;\r
1518 LIST_ENTRY *Next;\r
1519 DHCP_SERVICE *DhcpSb;\r
1520 DHCP_PROTOCOL *Instance;\r
1521 EFI_STATUS Status;\r
1522\r
1523 DhcpSb = (DHCP_SERVICE *) Context;\r
1524 Instance = DhcpSb->ActiveChild;\r
1525\r
1526 //\r
1527 // 0xffff is the maximum supported value for elapsed time according to RFC.\r
1528 //\r
1529 if (Instance != NULL && Instance->ElaspedTime < 0xffff) {\r
1530 Instance->ElaspedTime++;\r
1531 }\r
1532 \r
1533 //\r
1534 // Check the retransmit timer\r
1535 //\r
1536 if ((DhcpSb->PacketToLive > 0) && (--DhcpSb->PacketToLive == 0)) {\r
1537\r
1538 //\r
1539 // Select offer at each timeout if any offer received.\r
1540 //\r
1541 if (DhcpSb->DhcpState == Dhcp4Selecting && DhcpSb->LastOffer != NULL) {\r
1542\r
1543 Status = DhcpChooseOffer (DhcpSb);\r
1544\r
1545 if (EFI_ERROR(Status)) {\r
1546 if (DhcpSb->LastOffer != NULL) {\r
1547 FreePool (DhcpSb->LastOffer);\r
1548 DhcpSb->LastOffer = NULL;\r
1549 }\r
1550 } else {\r
1551 goto ON_EXIT;\r
1552 }\r
1553 }\r
1554 \r
1555 if (++DhcpSb->CurRetry < DhcpSb->MaxRetries) {\r
1556 //\r
1557 // Still has another try\r
1558 //\r
1559 DhcpRetransmit (DhcpSb);\r
1560 DhcpSetTransmitTimer (DhcpSb);\r
1561\r
1562 } else if (DHCP_CONNECTED (DhcpSb->DhcpState)) {\r
1563\r
1564 //\r
1565 // Retransmission failed, if the DHCP request is initiated by\r
1566 // user, adjust the current state according to the lease life.\r
1567 // Otherwise do nothing to wait the lease to timeout\r
1568 //\r
1569 if (DhcpSb->ExtraRefresh != 0) {\r
1570 Status = EFI_SUCCESS;\r
1571\r
1572 if (DhcpSb->LeaseLife < DhcpSb->T1) {\r
1573 Status = DhcpSetState (DhcpSb, Dhcp4Bound, FALSE);\r
1574\r
1575 } else if (DhcpSb->LeaseLife < DhcpSb->T2) {\r
1576 Status = DhcpSetState (DhcpSb, Dhcp4Renewing, FALSE);\r
1577\r
1578 } else if (DhcpSb->LeaseLife < DhcpSb->Lease) {\r
1579 Status = DhcpSetState (DhcpSb, Dhcp4Rebinding, FALSE);\r
1580\r
1581 } else {\r
1582 goto END_SESSION;\r
1583\r
1584 }\r
1585\r
1586 DhcpSb->IoStatus = EFI_TIMEOUT;\r
1587 DhcpNotifyUser (DhcpSb, DHCP_NOTIFY_RENEWREBIND);\r
1588 }\r
1589 } else {\r
1590 goto END_SESSION;\r
1591 }\r
1592 }\r
1593 \r
1594 //\r
1595 // If an address has been acquired, check whether need to\r
1596 // refresh or whether it has expired.\r
1597 //\r
1598 if (DHCP_CONNECTED (DhcpSb->DhcpState)) {\r
1599 DhcpSb->LeaseLife++;\r
1600\r
1601 //\r
1602 // Don't timeout the lease, only count the life if user is\r
1603 // requesting extra renew/rebind. Adjust the state after that.\r
1604 //\r
1605 if (DhcpSb->ExtraRefresh != 0) {\r
1606 return ;\r
1607 }\r
1608\r
1609 if (DhcpSb->LeaseLife == DhcpSb->Lease) {\r
1610 //\r
1611 // Lease expires, end the session\r
1612 //\r
1613 goto END_SESSION;\r
1614\r
1615 } else if (DhcpSb->LeaseLife == DhcpSb->T2) {\r
1616 //\r
1617 // T2 expires, transit to rebinding then send a REQUEST to any server\r
1618 //\r
1619 if (EFI_ERROR (DhcpSetState (DhcpSb, Dhcp4Rebinding, TRUE))) {\r
1620 goto END_SESSION;\r
1621 }\r
1622\r
1623 if (Instance != NULL) {\r
1624 Instance->ElaspedTime= 0;\r
1625 } \r
1626 \r
1627 Status = DhcpSendMessage (\r
1628 DhcpSb,\r
1629 DhcpSb->Selected,\r
1630 DhcpSb->Para,\r
1631 DHCP_MSG_REQUEST,\r
1632 NULL\r
1633 );\r
1634\r
1635 if (EFI_ERROR (Status)) {\r
1636 goto END_SESSION;\r
1637 }\r
1638\r
1639 } else if (DhcpSb->LeaseLife == DhcpSb->T1) {\r
1640 //\r
1641 // T1 expires, transit to renewing, then send a REQUEST to the server\r
1642 //\r
1643 if (EFI_ERROR (DhcpSetState (DhcpSb, Dhcp4Renewing, TRUE))) {\r
1644 goto END_SESSION;\r
1645 }\r
1646\r
1647 if (Instance != NULL) {\r
1648 Instance->ElaspedTime= 0;\r
1649 } \r
1650\r
1651 Status = DhcpSendMessage (\r
1652 DhcpSb,\r
1653 DhcpSb->Selected,\r
1654 DhcpSb->Para,\r
1655 DHCP_MSG_REQUEST,\r
1656 NULL\r
1657 );\r
1658\r
1659 if (EFI_ERROR (Status)) {\r
1660 goto END_SESSION;\r
1661 }\r
1662 }\r
1663 }\r
1664\r
1665ON_EXIT:\r
1666 //\r
1667 // Iterate through all the DhcpSb Children.\r
1668 //\r
1669 NET_LIST_FOR_EACH_SAFE (Entry, Next, &DhcpSb->Children) {\r
1670 Instance = NET_LIST_USER_STRUCT (Entry, DHCP_PROTOCOL, Link);\r
1671 \r
1672 if ((Instance != NULL) && (Instance->Token != NULL)) {\r
1673 Instance->Timeout--;\r
1674 if (Instance->Timeout == 0) {\r
1675 PxeDhcpDone (Instance);\r
1676 }\r
1677 }\r
1678 }\r
1679\r
1680 return ;\r
1681\r
1682END_SESSION:\r
1683 DhcpEndSession (DhcpSb, EFI_TIMEOUT);\r
1684\r
1685 return ;\r
1686}\r