1 /** @file
2   EFI DHCP protocol implementation.
3   RFCs supported are:
4   RFC 2131: Dynamic Host Configuration Protocol
5   RFC 2132: DHCP Options and BOOTP Vendor Extensions
6   RFC 1534: Interoperation Between DHCP and BOOTP
7   RFC 3396: Encoding Long Options in DHCP.
8 
9 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution.  The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
14 
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 
18 **/
19 
20 #ifndef __EFI_DHCP4_IMPL_H__
21 #define __EFI_DHCP4_IMPL_H__
22 
23 
24 
25 #include <Uefi.h>
26 
27 #include <Protocol/Dhcp4.h>
28 #include <Protocol/Udp4.h>
29 
30 #include <Library/DebugLib.h>
31 #include <Library/UefiDriverEntryPoint.h>
32 #include <Library/UefiBootServicesTableLib.h>
33 #include <Library/UefiLib.h>
34 #include <Library/BaseLib.h>
35 #include <Library/NetLib.h>
36 
37 typedef struct _DHCP_SERVICE  DHCP_SERVICE;
38 typedef struct _DHCP_PROTOCOL DHCP_PROTOCOL;
39 
40 #include "Dhcp4Option.h"
41 #include "Dhcp4Io.h"
42 
43 #define DHCP_SERVICE_SIGNATURE   SIGNATURE_32 ('D', 'H', 'C', 'P')
44 #define DHCP_PROTOCOL_SIGNATURE  SIGNATURE_32 ('d', 'h', 'c', 'p')
45 
46 
47 //
48 // The state of the DHCP service. It starts as UNCONFIGED. If
49 // and active child configures the service successfully, it
50 // goes to CONFIGED. If the active child configures NULL, it
51 // goes back to UNCONFIGED. It becomes DESTROY if it is (partly)
52 // destroyed.
53 //
54 #define DHCP_UNCONFIGED          0
55 #define DHCP_CONFIGED            1
56 #define DHCP_DESTROY             2
57 
58 
59 struct _DHCP_PROTOCOL {
60   UINT32                            Signature;
61   EFI_DHCP4_PROTOCOL                Dhcp4Protocol;
62   LIST_ENTRY                        Link;
63   EFI_HANDLE                        Handle;
64   DHCP_SERVICE                      *Service;
65 
66   BOOLEAN                           InDestroy;
67 
68   EFI_EVENT                         CompletionEvent;
69   EFI_EVENT                         RenewRebindEvent;
70 
71   EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN  *Token;
72   UDP_IO                            *UdpIo; // The UDP IO used for TransmitReceive.
73   UINT32                            Timeout;
74   UINT16                            ElaspedTime;
75   NET_BUF_QUEUE                     ResponseQueue;
76 };
77 
78 //
79 // DHCP driver is specical in that it is a singleton. Although it
80 // has a service binding, there can be only one active child.
81 //
82 struct _DHCP_SERVICE {
83   UINT32                        Signature;
84   EFI_SERVICE_BINDING_PROTOCOL  ServiceBinding;
85 
86   INTN                          ServiceState; // CONFIGED, UNCONFIGED, and DESTROY
87 
88   EFI_HANDLE                    Controller;
89   EFI_HANDLE                    Image;
90 
91   LIST_ENTRY                    Children;
92   UINTN                         NumChildren;
93 
94   INTN                          DhcpState;
95   EFI_STATUS                    IoStatus;     // the result of last user operation
96   UINT32                        Xid;
97 
98   IP4_ADDR                      ClientAddr;   // lease IP or configured client address
99   IP4_ADDR                      Netmask;
100   IP4_ADDR                      ServerAddr;
101 
102   EFI_DHCP4_PACKET              *LastOffer;   // The last received offer
103   EFI_DHCP4_PACKET              *Selected;
104   DHCP_PARAMETER                *Para;
105 
106   UINT32                        Lease;
107   UINT32                        T1;
108   UINT32                        T2;
109   INTN                          ExtraRefresh; // This refresh is reqested by user
110 
111   UDP_IO                        *UdpIo;       // Udp child receiving all DHCP message
112   UDP_IO                        *LeaseIoPort; // Udp child with lease IP
113   EFI_DHCP4_PACKET              *LastPacket;  // The last sent packet for retransmission
114   EFI_MAC_ADDRESS               Mac;
115   UINT8                         HwType;
116   UINT8                         HwLen;
117   UINT8                         ClientAddressSendOut[16];
118 
119   DHCP_PROTOCOL                 *ActiveChild;
120   EFI_DHCP4_CONFIG_DATA         ActiveConfig;
121   UINT32                        UserOptionLen;
122 
123   //
124   // Timer event and various timer
125   //
126   EFI_EVENT                     Timer;
127 
128   UINT32                        PacketToLive; // Retransmission timer for our packets
129   UINT32                        LastTimeout;  // Record the init value of PacketToLive every time
130   INTN                          CurRetry;
131   INTN                          MaxRetries;
132   UINT32                        LeaseLife;
133 };
134 
135 typedef struct {
136   EFI_DHCP4_PACKET_OPTION       **Option;
137   UINT32                        OptionCount;
138   UINT32                        Index;
139 } DHCP_PARSE_CONTEXT;
140 
141 #define DHCP_INSTANCE_FROM_THIS(Proto)  \
142   CR ((Proto), DHCP_PROTOCOL, Dhcp4Protocol, DHCP_PROTOCOL_SIGNATURE)
143 
144 #define DHCP_SERVICE_FROM_THIS(Sb)      \
145   CR ((Sb), DHCP_SERVICE, ServiceBinding, DHCP_SERVICE_SIGNATURE)
146 
147 extern EFI_DHCP4_PROTOCOL mDhcp4ProtocolTemplate;
148 
149 /**
150   Give up the control of the DHCP service to let other child
151   resume. Don't change the service's DHCP state and the Client
152   address and option list configure as required by RFC2131.
153 
154   @param  DhcpSb                 The DHCP service instance.
155 
156 **/
157 VOID
158 DhcpYieldControl (
159   IN DHCP_SERVICE           *DhcpSb
160   );
161 
162 /**
163   Complete a Dhcp4 transaction and signal the upper layer.
164 
165   @param Instance      Dhcp4 instance.
166 
167 **/
168 VOID
169 PxeDhcpDone (
170   IN DHCP_PROTOCOL  *Instance
171   );
172 
173 /**
174   Free the resource related to the configure parameters.
175   DHCP driver will make a copy of the user's configure
176   such as the time out value.
177 
178   @param  Config                 The DHCP configure data
179 
180 **/
181 VOID
182 DhcpCleanConfigure (
183   IN OUT EFI_DHCP4_CONFIG_DATA  *Config
184   );
185 
186 /**
187   Set the elapsed time based on the given instance and the pointer to the
188   elapsed time option.
189 
190   @param[in]      Elapsed       The pointer to the position to append.
191   @param[in]      Instance      The pointer to the Dhcp4 instance.
192 **/
193 VOID
194 SetElapsedTime (
195   IN     UINT16                 *Elapsed,
196   IN     DHCP_PROTOCOL          *Instance
197   );
198 
199 #endif
200