1 /** @file
2   The DHCP4 protocol implementation.
3 
4 Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution.  The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9 
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #ifndef __EFI_DHCP4_IO_H__
16 #define __EFI_DHCP4_IO_H__
17 
18 #include <Uefi.h>
19 
20 #include <Protocol/ServiceBinding.h>
21 
22 #include <Library/NetLib.h>
23 #include <Library/UdpIoLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 
27 
28 
29 #define DHCP_WAIT_OFFER                    3  // Time to wait the offers
30 #define DHCP_DEFAULT_LEASE  7 * 24 * 60 * 60  // Seven days as default.
31 #define DHCP_SERVER_PORT                  67
32 #define DHCP_CLIENT_PORT                  68
33 
34 //
35 // BOOTP header "op" field
36 //
37 #define BOOTP_REQUEST           1
38 #define BOOTP_REPLY             2
39 
40 //
41 // DHCP message types
42 //
43 #define DHCP_MSG_DISCOVER       1
44 #define DHCP_MSG_OFFER          2
45 #define DHCP_MSG_REQUEST        3
46 #define DHCP_MSG_DECLINE        4
47 #define DHCP_MSG_ACK            5
48 #define DHCP_MSG_NAK            6
49 #define DHCP_MSG_RELEASE        7
50 #define DHCP_MSG_INFORM         8
51 
52 //
53 // DHCP notify user type
54 //
55 #define DHCP_NOTIFY_COMPLETION  1
56 #define DHCP_NOTIFY_RENEWREBIND 2
57 #define DHCP_NOTIFY_ALL         3
58 
59 #define DHCP_IS_BOOTP(Parameter)  (((Parameter) == NULL) || ((Parameter)->DhcpType == 0))
60 
61 #define DHCP_CONNECTED(State)     \
62   (((State) == Dhcp4Bound) || ((State) == (Dhcp4Renewing)) || ((State) == Dhcp4Rebinding))
63 
64 /**
65   Set the DHCP state. If CallUser is true, it will try to notify
66   the user before change the state by DhcpNotifyUser. It returns
67   EFI_ABORTED if the user return EFI_ABORTED, otherwise, it returns
68   EFI_SUCCESS. If CallUser is FALSE, it isn't necessary to test
69   the return value of this function.
70 
71   @param  DhcpSb                The DHCP service instance
72   @param  State                 The new DHCP state to change to
73   @param  CallUser              Whether we need to call user
74 
75   @retval EFI_SUCCESS           The state is changed
76   @retval EFI_ABORTED           The user asks to abort the DHCP process.
77 
78 **/
79 EFI_STATUS
80 DhcpSetState (
81   IN OUT DHCP_SERVICE           *DhcpSb,
82   IN     INTN                   State,
83   IN     BOOLEAN                CallUser
84   );
85 
86 /**
87   Build and transmit a DHCP message according to the current states.
88   This function implement the Table 5. of RFC 2131. Always transits
89   the state (as defined in Figure 5. of the same RFC) before sending
90   a DHCP message. The table is adjusted accordingly.
91 
92   @param[in]  DhcpSb                The DHCP service instance
93   @param[in]  Seed                  The seed packet which the new packet is based on
94   @param[in]  Para                  The DHCP parameter of the Seed packet
95   @param[in]  Type                  The message type to send
96   @param[in]  Msg                   The human readable message to include in the packet
97                                     sent.
98 
99   @retval EFI_OUT_OF_RESOURCES  Failed to allocate resources for the packet
100   @retval EFI_ACCESS_DENIED     Failed to transmit the packet through UDP
101   @retval EFI_SUCCESS           The message is sent
102   @retval other                 Other error occurs
103 
104 **/
105 EFI_STATUS
106 DhcpSendMessage (
107   IN DHCP_SERVICE           *DhcpSb,
108   IN EFI_DHCP4_PACKET       *Seed,
109   IN DHCP_PARAMETER         *Para,
110   IN UINT8                  Type,
111   IN UINT8                  *Msg
112   );
113 
114 /**
115   Each DHCP service has three timer. Two of them are count down timer.
116   One for the packet retransmission. The other is to collect the offers.
117   The third timer increaments the lease life which is compared to T1, T2,
118   and lease to determine the time to renew and rebind the lease.
119   DhcpOnTimerTick will be called once every second.
120 
121   @param[in]  Event                 The timer event
122   @param[in]  Context               The context, which is the DHCP service instance.
123 
124 **/
125 VOID
126 EFIAPI
127 DhcpOnTimerTick (
128   IN EFI_EVENT              Event,
129   IN VOID                   *Context
130   );
131 
132 /**
133   Handle the received DHCP packets. This function drives the DHCP
134   state machine.
135 
136   @param  UdpPacket             The UDP packets received.
137   @param  EndPoint              The local/remote UDP access point
138   @param  IoStatus              The status of the UDP receive
139   @param  Context               The opaque parameter to the function.
140 
141 **/
142 VOID
143 EFIAPI
144 DhcpInput (
145   NET_BUF                   *UdpPacket,
146   UDP_END_POINT             *EndPoint,
147   EFI_STATUS                IoStatus,
148   VOID                      *Context
149   );
150 
151 /**
152   Send an initial DISCOVER or REQUEST message according to the
153   DHCP service's current state.
154 
155   @param[in]  DhcpSb                The DHCP service instance
156 
157   @retval EFI_SUCCESS           The request has been sent
158   @retval other                 Some error occurs when sending the request.
159 
160 **/
161 EFI_STATUS
162 DhcpInitRequest (
163   IN DHCP_SERVICE           *DhcpSb
164   );
165 
166 /**
167   Clean up the DHCP related states, IoStatus isn't reset.
168 
169   @param  DhcpSb                The DHCP instance service.
170 
171 **/
172 VOID
173 DhcpCleanLease (
174   IN DHCP_SERVICE           *DhcpSb
175   );
176 
177 /**
178   Release the net buffer when packet is sent.
179 
180   @param  UdpPacket             The UDP packets received.
181   @param  EndPoint              The local/remote UDP access point
182   @param  IoStatus              The status of the UDP receive
183   @param  Context               The opaque parameter to the function.
184 
185 **/
186 VOID
187 EFIAPI
188 DhcpOnPacketSent (
189   NET_BUF                   *Packet,
190   UDP_END_POINT             *EndPoint,
191   EFI_STATUS                IoStatus,
192   VOID                      *Context
193   );
194 
195 #endif
196