1 /** @file
2   Dhcp6 support functions declaration.
3 
4   Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
5 
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php.
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #ifndef __EFI_DHCP6_UTILITY_H__
17 #define __EFI_DHCP6_UTILITY_H__
18 
19 
20 #define  DHCP6_10_BIT_MASK             0x3ff
21 #define  DHCP6_DAD_ADDITIONAL_DELAY    30000000 // 3 seconds
22 
23 /**
24   Generate client Duid in the format of Duid-llt.
25 
26   @param[in]  Mode          The pointer to the mode of SNP.
27 
28   @retval     NULL          if failed to generate client Id.
29   @retval     Others        The pointer to the new client id.
30 
31 **/
32 EFI_DHCP6_DUID *
33 Dhcp6GenerateClientId (
34   IN EFI_SIMPLE_NETWORK_MODE    *Mode
35   );
36 
37 /**
38   Copy the Dhcp6 configure data.
39 
40   @param[in]  DstCfg        The pointer to the destination configure data.
41   @param[in]  SorCfg        The pointer to the source configure data.
42 
43   @retval EFI_SUCCESS           Copy the content from SorCfg from DstCfg successfully.
44   @retval EFI_OUT_OF_RESOURCES  Required system resources could not be allocated.
45 
46 **/
47 EFI_STATUS
48 Dhcp6CopyConfigData (
49   IN EFI_DHCP6_CONFIG_DATA     *DstCfg,
50   IN EFI_DHCP6_CONFIG_DATA     *SorCfg
51   );
52 
53 /**
54   Clean up the configure data.
55 
56   @param[in, out]  CfgData       The pointer to the configure data.
57 
58 **/
59 VOID
60 Dhcp6CleanupConfigData (
61   IN OUT EFI_DHCP6_CONFIG_DATA       *CfgData
62   );
63 
64 /**
65   Clean up the mode data.
66 
67   @param[in, out]  ModeData      The pointer to the mode data.
68 
69 **/
70 VOID
71 Dhcp6CleanupModeData (
72   IN OUT EFI_DHCP6_MODE_DATA        *ModeData
73   );
74 
75 /**
76   Calculate the expire time by the algorithm defined in rfc.
77 
78   @param[in]  Base          The base value of the time.
79   @param[in]  IsFirstRt     If TRUE, it is the first time to calculate expire time.
80   @param[in]  NeedSigned    If TRUE, the the signed factor is needed.
81 
82   @return     Expire        The calculated result for the new expire time.
83 
84 **/
85 UINT32
86 Dhcp6CalculateExpireTime (
87   IN UINT32                    Base,
88   IN BOOLEAN                   IsFirstRt,
89   IN BOOLEAN                   NeedSigned
90   );
91 
92 /**
93   Calculate the lease time by the algorithm defined in rfc.
94 
95   @param[in]  IaCb          The pointer to the Ia control block.
96 
97 **/
98 VOID
99 Dhcp6CalculateLeaseTime (
100   IN DHCP6_IA_CB               *IaCb
101   );
102 
103 /**
104   Check whether the addresses are all included by the configured Ia.
105 
106   @param[in]  Ia            The pointer to the Ia.
107   @param[in]  AddressCount  The number of addresses.
108   @param[in]  Addresses     The pointer to the addresses buffer.
109 
110   @retval EFI_SUCCESS         The addresses are all included by the configured IA.
111   @retval EFI_NOT_FOUND       The addresses are not included by the configured IA.
112 
113 **/
114 EFI_STATUS
115 Dhcp6CheckAddress (
116   IN EFI_DHCP6_IA              *Ia,
117   IN UINT32                    AddressCount,
118   IN EFI_IPv6_ADDRESS          *Addresses
119   );
120 
121 /**
122   Deprive the addresses from current Ia, and generate another eliminated Ia.
123 
124   @param[in]  Ia            The pointer to the Ia.
125   @param[in]  AddressCount  The number of addresses.
126   @param[in]  Addresses     The pointer to the addresses buffer.
127 
128   @retval     NULL          If failed to generate the deprived Ia.
129   @retval     others        The pointer to the deprived Ia.
130 
131 **/
132 EFI_DHCP6_IA *
133 Dhcp6DepriveAddress (
134   IN EFI_DHCP6_IA              *Ia,
135   IN UINT32                    AddressCount,
136   IN EFI_IPv6_ADDRESS          *Addresses
137   );
138 
139 /**
140   The dummy ext buffer free callback routine.
141 
142   @param[in]  Arg           The pointer to the parameter.
143 
144 **/
145 VOID
146 EFIAPI
147 Dhcp6DummyExtFree (
148   IN VOID                      *Arg
149   );
150 
151 /**
152   The callback routine once message transmitted.
153 
154   @param[in]  Wrap          The pointer to the received net buffer.
155   @param[in]  EndPoint      The pointer to the udp end point.
156   @param[in]  IoStatus      The return status from udp io.
157   @param[in]  Context       The opaque parameter to the function.
158 
159 **/
160 VOID
161 EFIAPI
162 Dhcp6OnTransmitted (
163   IN NET_BUF                   *Wrap,
164   IN UDP_END_POINT             *EndPoint,
165   IN EFI_STATUS                IoStatus,
166   IN VOID                      *Context
167   );
168 
169 /**
170   Append the appointed option to the buf, and move the buf to the end.
171 
172   @param[in, out] Buf           The pointer to buffer.
173   @param[in]      OptType       The option type.
174   @param[in]      OptLen        The lenght of option content.s
175   @param[in]      Data          The pointer to the option content.
176 
177   @return         Buf           The position to append the next option.
178 
179 **/
180 UINT8 *
181 Dhcp6AppendOption (
182   IN OUT UINT8                 *Buf,
183   IN     UINT16                OptType,
184   IN     UINT16                OptLen,
185   IN     UINT8                 *Data
186   );
187 
188 /**
189   Append the Ia option to Buf, and move Buf to the end.
190 
191   @param[in, out] Buf           The pointer to the position to append.
192   @param[in]      Ia            The pointer to the Ia.
193   @param[in]      T1            The time of T1.
194   @param[in]      T2            The time of T2.
195   @param[in]      MessageType   Message type of DHCP6 package.
196 
197   @return         Buf           The position to append the next Ia option.
198 
199 **/
200 UINT8 *
201 Dhcp6AppendIaOption (
202   IN OUT UINT8                  *Buf,
203   IN     EFI_DHCP6_IA           *Ia,
204   IN     UINT32                 T1,
205   IN     UINT32                 T2,
206   IN     UINT32                 MessageType
207   );
208 
209 /**
210   Append the appointed Elapsed time option to Buf, and move Buf to the end.
211 
212   @param[in, out] Buf           The pointer to the position to append.
213   @param[in]      Instance      The pointer to the Dhcp6 instance.
214   @param[out]     Elapsed       The pointer to the elapsed time value in
215                                   the generated packet.
216 
217   @return         Buf           The position to append the next Ia option.
218 
219 **/
220 UINT8 *
221 Dhcp6AppendETOption (
222   IN OUT UINT8                  *Buf,
223   IN     DHCP6_INSTANCE         *Instance,
224   OUT    UINT16                 **Elapsed
225   );
226 
227 /**
228   Set the elapsed time based on the given instance and the pointer to the
229   elapsed time option.
230 
231   @param[in]      Elapsed       The pointer to the position to append.
232   @param[in]      Instance      The pointer to the Dhcp6 instance.
233 **/
234 VOID
235 SetElapsedTime (
236   IN     UINT16                 *Elapsed,
237   IN     DHCP6_INSTANCE         *Instance
238   );
239 
240 /**
241   Seek the address of the first byte of the option header.
242 
243   @param[in]  Buf           The pointer to buffer.
244   @param[in]  SeekLen       The length to seek.
245   @param[in]  OptType       The option type.
246 
247   @retval     NULL          If failed to seek the option.
248   @retval     others        The position to the option.
249 
250 **/
251 UINT8 *
252 Dhcp6SeekOption (
253   IN UINT8                     *Buf,
254   IN UINT32                    SeekLen,
255   IN UINT16                    OptType
256   );
257 
258 /**
259   Seek the address of the first byte of the Ia option header.
260 
261   @param[in]  Buf           The pointer to the buffer.
262   @param[in]  SeekLen       The length to seek.
263   @param[in]  IaDesc        The pointer to the Ia descriptor.
264 
265   @retval     NULL          If failed to seek the Ia option.
266   @retval     others        The position to the Ia option.
267 
268 **/
269 UINT8 *
270 Dhcp6SeekIaOption (
271   IN UINT8                     *Buf,
272   IN UINT32                    SeekLen,
273   IN EFI_DHCP6_IA_DESCRIPTOR   *IaDesc
274   );
275 
276 /**
277   Parse the address option and update the address info.
278 
279   @param[in]      CurrentIa     The pointer to the Ia Address in control blcok.
280   @param[in]      IaInnerOpt    The pointer to the buffer.
281   @param[in]      IaInnerLen    The length to parse.
282   @param[out]     AddrNum       The number of addresses.
283   @param[in, out] AddrBuf       The pointer to the address buffer.
284 
285 **/
286 VOID
287 Dhcp6ParseAddrOption (
288   IN     EFI_DHCP6_IA            *CurrentIa,
289   IN     UINT8                   *IaInnerOpt,
290   IN     UINT16                  IaInnerLen,
291      OUT UINT32                  *AddrNum,
292   IN OUT EFI_DHCP6_IA_ADDRESS    *AddrBuf
293   );
294 
295 /**
296   Create a control blcok for the Ia according to the corresponding options.
297 
298   @param[in]  Instance              The pointer to DHCP6 Instance.
299   @param[in]  IaInnerOpt            The pointer to the inner options in the Ia option.
300   @param[in]  IaInnerLen            The length of all the inner options in the Ia option.
301   @param[in]  T1                    T1 time in the Ia option.
302   @param[in]  T2                    T2 time in the Ia option.
303 
304   @retval     EFI_NOT_FOUND         No valid IA option is found.
305   @retval     EFI_SUCCESS           Create an IA control block successfully.
306   @retval     EFI_OUT_OF_RESOURCES  Required system resources could not be allocated.
307   @retval     EFI_DEVICE_ERROR      An unexpected error.
308 
309 **/
310 EFI_STATUS
311 Dhcp6GenerateIaCb (
312   IN  DHCP6_INSTANCE           *Instance,
313   IN  UINT8                    *IaInnerOpt,
314   IN  UINT16                   IaInnerLen,
315   IN  UINT32                   T1,
316   IN  UINT32                   T2
317   );
318 
319 
320 /**
321   Cache the current IA configuration information.
322 
323   @param[in] Instance           The pointer to DHCP6 Instance.
324 
325   @retval EFI_SUCCESS           Cache the current IA successfully.
326   @retval EFI_OUT_OF_RESOURCES  Required system resources could not be allocated.
327 
328 **/
329 EFI_STATUS
330 Dhcp6CacheIa (
331   IN DHCP6_INSTANCE           *Instance
332   );
333 
334 
335 /**
336   Append CacheIa to the currrent IA. Meanwhile, clear CacheIa.ValidLifetime to 0.
337 
338   @param[in]  Instance            The pointer to DHCP6 instance.
339 
340 **/
341 VOID
342 Dhcp6AppendCacheIa (
343   IN DHCP6_INSTANCE           *Instance
344   );
345 
346 /**
347   Calculate the Dhcp6 get mapping timeout by adding additinal delay to the IP6 DAD transmits count.
348 
349   @param[in]   Ip6Cfg              The pointer to Ip6 config protocol.
350   @param[out]  TimeOut             The time out value in 100ns units.
351 
352   @retval   EFI_INVALID_PARAMETER  Input parameters are invalid.
353   @retval   EFI_SUCCESS            Calculate the time out value successfully.
354 **/
355 EFI_STATUS
356 Dhcp6GetMappingTimeOut (
357   IN  EFI_IP6_CONFIG_PROTOCOL       *Ip6Cfg,
358   OUT UINTN                         *TimeOut
359   );
360 #endif
361