]>
Commit | Line | Data |
---|---|---|
8a67d61d | 1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2006 - 2007, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | Udp4Impl.c\r | |
15 | \r | |
16 | Abstract:\r | |
17 | \r | |
18 | The implementation of the Udp4 protocol.\r | |
19 | \r | |
20 | \r | |
21 | **/\r | |
22 | \r | |
23 | \r | |
24 | #include "Udp4Impl.h"\r | |
25 | \r | |
26 | UINT16 mUdp4RandomPort;\r | |
27 | \r | |
28 | STATIC\r | |
29 | VOID\r | |
30 | EFIAPI\r | |
31 | Udp4CheckTimeout (\r | |
32 | IN EFI_EVENT Event,\r | |
33 | IN VOID *Context\r | |
34 | );\r | |
35 | \r | |
36 | STATIC\r | |
37 | BOOLEAN\r | |
38 | Udp4FindInstanceByPort (\r | |
39 | IN NET_LIST_ENTRY *InstanceList,\r | |
40 | IN EFI_IPv4_ADDRESS *Address,\r | |
41 | IN UINT16 Port\r | |
42 | );\r | |
43 | \r | |
44 | STATIC\r | |
45 | VOID\r | |
46 | Udp4DgramSent (\r | |
47 | IN EFI_STATUS Status,\r | |
48 | IN VOID *Context,\r | |
49 | IN VOID *Sender,\r | |
50 | IN VOID *NotifyData\r | |
51 | );\r | |
52 | \r | |
53 | STATIC\r | |
54 | VOID\r | |
55 | Udp4DgramRcvd (\r | |
56 | IN EFI_STATUS Status,\r | |
57 | IN ICMP_ERROR IcmpError,\r | |
58 | IN EFI_NET_SESSION_DATA *NetSession,\r | |
59 | IN NET_BUF *Packet,\r | |
60 | IN VOID *Context\r | |
61 | );\r | |
62 | \r | |
63 | STATIC\r | |
64 | EFI_STATUS\r | |
65 | Udp4CancelTokens (\r | |
66 | IN NET_MAP *Map,\r | |
67 | IN NET_MAP_ITEM *Item,\r | |
68 | IN VOID *Arg OPTIONAL\r | |
69 | );\r | |
70 | \r | |
71 | STATIC\r | |
72 | BOOLEAN\r | |
73 | Udp4MatchDgram (\r | |
74 | IN UDP4_INSTANCE_DATA *Instance,\r | |
75 | IN EFI_UDP4_SESSION_DATA *Udp4Session\r | |
76 | );\r | |
77 | \r | |
78 | STATIC\r | |
79 | VOID\r | |
80 | EFIAPI\r | |
81 | Udp4RecycleRxDataWrap (\r | |
82 | IN EFI_EVENT Event,\r | |
83 | IN VOID *Context\r | |
84 | );\r | |
85 | \r | |
86 | STATIC\r | |
87 | UDP4_RXDATA_WRAP *\r | |
88 | Udp4WrapRxData (\r | |
89 | IN UDP4_INSTANCE_DATA *Instance,\r | |
90 | IN NET_BUF *Packet,\r | |
91 | IN EFI_UDP4_RECEIVE_DATA *RxData\r | |
92 | );\r | |
93 | \r | |
94 | STATIC\r | |
95 | UINTN\r | |
96 | Udp4EnqueueDgram (\r | |
97 | IN UDP4_SERVICE_DATA *Udp4Service,\r | |
98 | IN NET_BUF *Packet,\r | |
99 | IN EFI_UDP4_RECEIVE_DATA *RxData\r | |
100 | );\r | |
101 | \r | |
102 | STATIC\r | |
103 | VOID\r | |
104 | Udp4DeliverDgram (\r | |
105 | IN UDP4_SERVICE_DATA *Udp4Service\r | |
106 | );\r | |
107 | \r | |
108 | STATIC\r | |
109 | VOID\r | |
110 | Udp4Demultiplex (\r | |
111 | IN UDP4_SERVICE_DATA *Udp4Service,\r | |
112 | IN EFI_NET_SESSION_DATA *NetSession,\r | |
113 | IN NET_BUF *Packet\r | |
114 | );\r | |
115 | \r | |
116 | STATIC\r | |
117 | VOID\r | |
118 | Udp4IcmpHandler (\r | |
119 | IN UDP4_SERVICE_DATA *Udp4Service,\r | |
120 | IN ICMP_ERROR IcmpError,\r | |
121 | IN EFI_NET_SESSION_DATA *NetSession,\r | |
122 | IN NET_BUF *Packet\r | |
123 | );\r | |
124 | \r | |
125 | STATIC\r | |
126 | VOID\r | |
127 | Udp4SendPortUnreach (\r | |
128 | IN IP_IO *IpIo,\r | |
129 | IN EFI_NET_SESSION_DATA *NetSession,\r | |
130 | IN VOID *Udp4Header\r | |
131 | );\r | |
132 | \r | |
133 | \r | |
134 | /**\r | |
135 | Create the Udp service context data.\r | |
136 | \r | |
137 | @param Udp4Service Pointer to the UDP4_SERVICE_DATA.\r | |
138 | @param ImageHandle The image handle of this udp4 driver.\r | |
139 | @param ControllerHandle The controller handle this udp4 driver binds on.\r | |
140 | \r | |
141 | @retval EFI_SUCCESS The udp4 service context data is created and\r | |
142 | initialized.\r | |
143 | @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.\r | |
144 | \r | |
145 | **/\r | |
146 | EFI_STATUS\r | |
147 | Udp4CreateService (\r | |
148 | IN UDP4_SERVICE_DATA *Udp4Service,\r | |
149 | IN EFI_HANDLE ImageHandle,\r | |
150 | IN EFI_HANDLE ControllerHandle\r | |
151 | )\r | |
152 | {\r | |
153 | EFI_STATUS Status;\r | |
154 | IP_IO_OPEN_DATA OpenData;\r | |
155 | \r | |
156 | Udp4Service->Signature = UDP4_SERVICE_DATA_SIGNATURE;\r | |
157 | Udp4Service->ServiceBinding = mUdp4ServiceBinding;\r | |
158 | Udp4Service->ImageHandle = ImageHandle;\r | |
159 | Udp4Service->ControllerHandle = ControllerHandle;\r | |
160 | Udp4Service->ChildrenNumber = 0;\r | |
161 | \r | |
162 | NetListInit (&Udp4Service->ChildrenList);\r | |
163 | \r | |
164 | //\r | |
165 | // Create the IpIo for this service context.\r | |
166 | //\r | |
167 | Udp4Service->IpIo = IpIoCreate (ImageHandle, ControllerHandle);\r | |
168 | if (Udp4Service->IpIo == NULL) {\r | |
169 | return EFI_OUT_OF_RESOURCES;\r | |
170 | }\r | |
171 | \r | |
172 | //\r | |
173 | // Set the OpenData used to open the IpIo.\r | |
174 | //\r | |
687a2e5f | 175 | CopyMem (&OpenData.IpConfigData, &mIpIoDefaultIpConfigData, sizeof (OpenData.IpConfigData));\r |
8a67d61d | 176 | OpenData.IpConfigData.AcceptBroadcast = TRUE;\r |
177 | OpenData.RcvdContext = (VOID *) Udp4Service;\r | |
178 | OpenData.SndContext = NULL;\r | |
179 | OpenData.PktRcvdNotify = Udp4DgramRcvd;\r | |
180 | OpenData.PktSentNotify = Udp4DgramSent;\r | |
181 | \r | |
182 | //\r | |
183 | // Configure and start the IpIo.\r | |
184 | //\r | |
185 | Status = IpIoOpen (Udp4Service->IpIo, &OpenData);\r | |
186 | if (EFI_ERROR (Status)) {\r | |
187 | goto RELEASE_IPIO;\r | |
188 | }\r | |
189 | \r | |
190 | //\r | |
191 | // Create the event for Udp timeout checking.\r | |
192 | //\r | |
193 | Status = gBS->CreateEvent (\r | |
194 | EVT_TIMER | EVT_NOTIFY_SIGNAL,\r | |
195 | NET_TPL_FAST_TIMER,\r | |
196 | Udp4CheckTimeout,\r | |
197 | Udp4Service,\r | |
198 | &Udp4Service->TimeoutEvent\r | |
199 | );\r | |
200 | if (EFI_ERROR (Status)) {\r | |
201 | goto RELEASE_IPIO;\r | |
202 | }\r | |
203 | \r | |
204 | //\r | |
205 | // Start the timeout timer event.\r | |
206 | //\r | |
207 | Status = gBS->SetTimer (\r | |
208 | Udp4Service->TimeoutEvent,\r | |
209 | TimerPeriodic,\r | |
210 | UDP4_TIMEOUT_INTERVAL\r | |
211 | );\r | |
212 | if (EFI_ERROR (Status)) {\r | |
213 | goto RELEASE_ALL;\r | |
214 | }\r | |
215 | \r | |
216 | Udp4Service->MacString = NULL;\r | |
217 | \r | |
218 | return EFI_SUCCESS;\r | |
219 | \r | |
220 | RELEASE_ALL:\r | |
221 | \r | |
222 | gBS->CloseEvent (Udp4Service->TimeoutEvent);\r | |
223 | \r | |
224 | RELEASE_IPIO:\r | |
225 | \r | |
226 | IpIoDestroy (Udp4Service->IpIo);\r | |
227 | \r | |
228 | return Status;\r | |
229 | }\r | |
230 | \r | |
231 | \r | |
232 | /**\r | |
233 | Clean the Udp service context data.\r | |
234 | \r | |
235 | @param Udp4Service Pointer to the UDP4_SERVICE_DATA.\r | |
236 | \r | |
237 | @return None.\r | |
238 | \r | |
239 | **/\r | |
240 | VOID\r | |
241 | Udp4CleanService (\r | |
242 | IN UDP4_SERVICE_DATA *Udp4Service\r | |
243 | )\r | |
244 | {\r | |
245 | //\r | |
246 | // Cancel the TimeoutEvent timer.\r | |
247 | //\r | |
248 | gBS->SetTimer (Udp4Service->TimeoutEvent, TimerCancel, 0);\r | |
249 | \r | |
250 | //\r | |
251 | // Close the TimeoutEvent timer.\r | |
252 | //\r | |
253 | gBS->CloseEvent (Udp4Service->TimeoutEvent);\r | |
254 | \r | |
255 | //\r | |
256 | // Destroy the IpIo.\r | |
257 | //\r | |
258 | IpIoDestroy (Udp4Service->IpIo);\r | |
259 | }\r | |
260 | \r | |
261 | \r | |
262 | /**\r | |
263 | This function checks and timeouts the I/O datagrams holding by the corresponding\r | |
264 | service context.\r | |
265 | \r | |
266 | @param Event The event this function registered to.\r | |
267 | @param Conext The context data registered during the creation of\r | |
268 | the Event.\r | |
269 | \r | |
270 | @return None.\r | |
271 | \r | |
272 | **/\r | |
273 | STATIC\r | |
274 | VOID\r | |
275 | EFIAPI\r | |
276 | Udp4CheckTimeout (\r | |
277 | IN EFI_EVENT Event,\r | |
278 | IN VOID *Context\r | |
279 | )\r | |
280 | {\r | |
281 | UDP4_SERVICE_DATA *Udp4Service;\r | |
282 | NET_LIST_ENTRY *Entry;\r | |
283 | UDP4_INSTANCE_DATA *Instance;\r | |
284 | NET_LIST_ENTRY *WrapEntry;\r | |
285 | NET_LIST_ENTRY *NextEntry;\r | |
286 | UDP4_RXDATA_WRAP *Wrap;\r | |
287 | \r | |
288 | Udp4Service = (UDP4_SERVICE_DATA *) Context;\r | |
289 | NET_CHECK_SIGNATURE (Udp4Service, UDP4_SERVICE_DATA_SIGNATURE);\r | |
290 | \r | |
291 | NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r | |
292 | //\r | |
293 | // Iterate all the instances belonging to this service context.\r | |
294 | //\r | |
295 | Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);\r | |
296 | NET_CHECK_SIGNATURE (Instance, UDP4_INSTANCE_DATA_SIGNATURE);\r | |
297 | \r | |
298 | if (!Instance->Configured || (Instance->ConfigData.ReceiveTimeout == 0)) {\r | |
299 | //\r | |
300 | // Skip this instance if it's not configured or no receive timeout.\r | |
301 | //\r | |
302 | continue;\r | |
303 | }\r | |
304 | \r | |
305 | NET_LIST_FOR_EACH_SAFE (WrapEntry, NextEntry, &Instance->RcvdDgramQue) {\r | |
306 | //\r | |
307 | // Iterate all the rxdatas belonging to this udp instance.\r | |
308 | //\r | |
309 | Wrap = NET_LIST_USER_STRUCT (Entry, UDP4_RXDATA_WRAP, Link);\r | |
310 | \r | |
311 | if (Wrap->TimeoutTick <= UDP4_TIMEOUT_INTERVAL / 1000) {\r | |
312 | //\r | |
313 | // Remove this RxData if it timeouts.\r | |
314 | //\r | |
315 | Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap);\r | |
316 | } else {\r | |
317 | Wrap->TimeoutTick -= UDP4_TIMEOUT_INTERVAL / 1000;\r | |
318 | }\r | |
319 | }\r | |
320 | }\r | |
321 | }\r | |
322 | \r | |
323 | \r | |
324 | /**\r | |
325 | This function intializes the new created udp instance.\r | |
326 | \r | |
327 | @param Udp4Service Pointer to the UDP4_SERVICE_DATA.\r | |
328 | @param Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.\r | |
329 | \r | |
330 | @return None.\r | |
331 | \r | |
332 | **/\r | |
333 | VOID\r | |
334 | Udp4InitInstance (\r | |
335 | IN UDP4_SERVICE_DATA *Udp4Service,\r | |
336 | IN UDP4_INSTANCE_DATA *Instance\r | |
337 | )\r | |
338 | {\r | |
339 | //\r | |
340 | // Set the signature.\r | |
341 | //\r | |
342 | Instance->Signature = UDP4_INSTANCE_DATA_SIGNATURE;\r | |
343 | \r | |
344 | //\r | |
345 | // Init the lists.\r | |
346 | //\r | |
347 | NetListInit (&Instance->Link);\r | |
348 | NetListInit (&Instance->RcvdDgramQue);\r | |
349 | NetListInit (&Instance->DeliveredDgramQue);\r | |
350 | \r | |
351 | //\r | |
352 | // Init the NET_MAPs.\r | |
353 | //\r | |
354 | NetMapInit (&Instance->TxTokens);\r | |
355 | NetMapInit (&Instance->RxTokens);\r | |
356 | NetMapInit (&Instance->McastIps);\r | |
357 | \r | |
358 | //\r | |
359 | // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members.\r | |
360 | //\r | |
361 | Instance->Udp4Service = Udp4Service;\r | |
687a2e5f | 362 | CopyMem (&Instance->Udp4Proto, &mUdp4Protocol, sizeof (Instance->Udp4Proto));\r |
8a67d61d | 363 | Instance->IcmpError = EFI_SUCCESS;\r |
364 | Instance->Configured = FALSE;\r | |
365 | Instance->IsNoMapping = FALSE;\r | |
366 | Instance->Destroyed = FALSE;\r | |
367 | }\r | |
368 | \r | |
369 | \r | |
370 | /**\r | |
371 | This function cleans the udp instance.\r | |
372 | \r | |
373 | @param Instance Pointer to the UDP4_INSTANCE_DATA to clean.\r | |
374 | \r | |
375 | @return None.\r | |
376 | \r | |
377 | **/\r | |
378 | VOID\r | |
379 | Udp4CleanInstance (\r | |
380 | IN UDP4_INSTANCE_DATA *Instance\r | |
381 | )\r | |
382 | {\r | |
383 | NetMapClean (&Instance->McastIps);\r | |
384 | NetMapClean (&Instance->RxTokens);\r | |
385 | NetMapClean (&Instance->TxTokens);\r | |
386 | }\r | |
387 | \r | |
388 | \r | |
389 | /**\r | |
390 | This function finds the udp instance by the specified <Address, Port> pair.\r | |
391 | \r | |
392 | @param InstanceList Pointer to the head of the list linking the udp\r | |
393 | instances.\r | |
394 | @param Address Pointer to the specified IPv4 address.\r | |
395 | @param Port The udp port number.\r | |
396 | \r | |
397 | @return Is the specified <Address, Port> pair found or not.\r | |
398 | \r | |
399 | **/\r | |
400 | STATIC\r | |
401 | BOOLEAN\r | |
402 | Udp4FindInstanceByPort (\r | |
403 | IN NET_LIST_ENTRY *InstanceList,\r | |
404 | IN EFI_IPv4_ADDRESS *Address,\r | |
405 | IN UINT16 Port\r | |
406 | )\r | |
407 | {\r | |
408 | NET_LIST_ENTRY *Entry;\r | |
409 | UDP4_INSTANCE_DATA *Instance;\r | |
410 | EFI_UDP4_CONFIG_DATA *ConfigData;\r | |
411 | \r | |
412 | NET_LIST_FOR_EACH (Entry, InstanceList) {\r | |
413 | //\r | |
414 | // Iterate all the udp instances.\r | |
415 | //\r | |
416 | Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);\r | |
417 | ConfigData = &Instance->ConfigData;\r | |
418 | \r | |
419 | if (!Instance->Configured || ConfigData->AcceptAnyPort) {\r | |
420 | //\r | |
421 | // If the instance is not configured or the configdata of the instance indicates\r | |
422 | // this instance accepts any port, skip it.\r | |
423 | //\r | |
424 | continue;\r | |
425 | }\r | |
426 | \r | |
84b5c78e | 427 | if (EFI_IP4_EQUAL (&ConfigData->StationAddress, Address) &&\r |
8a67d61d | 428 | (ConfigData->StationPort == Port)) {\r |
429 | //\r | |
430 | // if both the address and the port are the same, return TRUE.\r | |
431 | //\r | |
432 | return TRUE;\r | |
433 | }\r | |
434 | }\r | |
435 | \r | |
436 | //\r | |
437 | // return FALSE when matching fails.\r | |
438 | //\r | |
439 | return FALSE;\r | |
440 | }\r | |
441 | \r | |
442 | \r | |
443 | /**\r | |
444 | This function tries to bind the udp instance according to the configured port\r | |
445 | allocation stragety.\r | |
446 | \r | |
447 | @param InstanceList Pointer to the head of the list linking the udp\r | |
448 | instances.\r | |
449 | @param ConfigData Pointer to the ConfigData of the instance to be\r | |
450 | bound.\r | |
451 | \r | |
452 | @retval EFI_SUCCESS The bound operation is completed successfully.\r | |
453 | @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is\r | |
454 | already used by other instance.\r | |
455 | @retval EFI_OUT_OF_RESOURCES No available port resources.\r | |
456 | \r | |
457 | **/\r | |
458 | EFI_STATUS\r | |
459 | Udp4Bind (\r | |
460 | IN NET_LIST_ENTRY *InstanceList,\r | |
461 | IN EFI_UDP4_CONFIG_DATA *ConfigData\r | |
462 | )\r | |
463 | {\r | |
464 | EFI_IPv4_ADDRESS *StationAddress;\r | |
465 | UINT16 StartPort;\r | |
466 | \r | |
467 | if (ConfigData->AcceptAnyPort) {\r | |
468 | return EFI_SUCCESS;\r | |
469 | }\r | |
470 | \r | |
471 | StationAddress = &ConfigData->StationAddress;\r | |
472 | \r | |
473 | if (ConfigData->StationPort != 0) {\r | |
474 | \r | |
475 | if (!ConfigData->AllowDuplicatePort &&\r | |
476 | Udp4FindInstanceByPort (InstanceList, StationAddress, ConfigData->StationPort)) {\r | |
477 | //\r | |
478 | // Do not allow duplicate port and the port is already used by other instance.\r | |
479 | //\r | |
480 | return EFI_ACCESS_DENIED;\r | |
481 | }\r | |
482 | } else {\r | |
483 | //\r | |
484 | // select a random port for this instance;\r | |
485 | //\r | |
486 | \r | |
487 | if (ConfigData->AllowDuplicatePort) {\r | |
488 | //\r | |
489 | // Just pick up the random port if the instance allows duplicate port.\r | |
490 | //\r | |
491 | ConfigData->StationPort = mUdp4RandomPort;\r | |
492 | } else {\r | |
493 | \r | |
494 | StartPort = mUdp4RandomPort;\r | |
495 | \r | |
496 | while (Udp4FindInstanceByPort(InstanceList, StationAddress, mUdp4RandomPort)) {\r | |
497 | \r | |
498 | mUdp4RandomPort++;\r | |
499 | if (mUdp4RandomPort == 0) {\r | |
500 | mUdp4RandomPort = UDP4_PORT_KNOWN;\r | |
501 | }\r | |
502 | \r | |
503 | if (mUdp4RandomPort == StartPort) {\r | |
504 | //\r | |
505 | // No available port.\r | |
506 | //\r | |
507 | return EFI_OUT_OF_RESOURCES;\r | |
508 | }\r | |
509 | }\r | |
510 | \r | |
511 | ConfigData->StationPort = mUdp4RandomPort;\r | |
512 | }\r | |
513 | \r | |
514 | mUdp4RandomPort++;\r | |
515 | if (mUdp4RandomPort == 0) {\r | |
516 | mUdp4RandomPort = UDP4_PORT_KNOWN;\r | |
517 | }\r | |
518 | }\r | |
519 | \r | |
520 | return EFI_SUCCESS;\r | |
521 | }\r | |
522 | \r | |
523 | \r | |
524 | /**\r | |
525 | This function is used to check whether the NewConfigData has any un-reconfigurable\r | |
526 | parameters changed compared to the OldConfigData.\r | |
527 | \r | |
528 | @param OldConfigData Pointer to the current ConfigData the udp instance\r | |
529 | uses.\r | |
530 | @param NewConfigData Pointer to the new ConfigData.\r | |
531 | \r | |
532 | @return The instance is reconfigurable or not according to the NewConfigData.\r | |
533 | \r | |
534 | **/\r | |
535 | BOOLEAN\r | |
536 | Udp4IsReconfigurable (\r | |
537 | IN EFI_UDP4_CONFIG_DATA *OldConfigData,\r | |
538 | IN EFI_UDP4_CONFIG_DATA *NewConfigData\r | |
539 | )\r | |
540 | {\r | |
541 | if ((NewConfigData->AcceptAnyPort != OldConfigData->AcceptAnyPort) ||\r | |
542 | (NewConfigData->AcceptBroadcast != OldConfigData->AcceptBroadcast) ||\r | |
543 | (NewConfigData->AcceptPromiscuous != OldConfigData->AcceptPromiscuous) ||\r | |
544 | (NewConfigData->AllowDuplicatePort != OldConfigData->AllowDuplicatePort)) {\r | |
545 | //\r | |
546 | // The receiving filter parameters cannot be changed.\r | |
547 | //\r | |
548 | return FALSE;\r | |
549 | }\r | |
550 | \r | |
551 | if ((!NewConfigData->AcceptAnyPort) &&\r | |
552 | (NewConfigData->StationPort != OldConfigData->StationPort)) {\r | |
553 | //\r | |
554 | // The port is not changeable.\r | |
555 | //\r | |
556 | return FALSE;\r | |
557 | }\r | |
558 | \r | |
559 | if (!NewConfigData->AcceptPromiscuous) {\r | |
560 | \r | |
561 | if (NewConfigData->UseDefaultAddress != OldConfigData->UseDefaultAddress) {\r | |
562 | //\r | |
563 | // The NewConfigData differs to the old one on the UseDefaultAddress.\r | |
564 | //\r | |
565 | return FALSE;\r | |
566 | }\r | |
567 | \r | |
568 | if (!NewConfigData->UseDefaultAddress &&\r | |
84b5c78e | 569 | (!EFI_IP4_EQUAL (&NewConfigData->StationAddress, &OldConfigData->StationAddress) ||\r |
570 | !EFI_IP4_EQUAL (&NewConfigData->SubnetMask, &OldConfigData->SubnetMask))) {\r | |
8a67d61d | 571 | //\r |
572 | // If the instance doesn't use the default address, and the new address or\r | |
573 | // new subnet mask is different from the old values.\r | |
574 | //\r | |
575 | return FALSE;\r | |
576 | }\r | |
577 | }\r | |
578 | \r | |
84b5c78e | 579 | if (!EFI_IP4_EQUAL (&NewConfigData->RemoteAddress, &OldConfigData->RemoteAddress)) {\r |
8a67d61d | 580 | //\r |
581 | // The remoteaddress is not the same.\r | |
582 | //\r | |
583 | return FALSE;\r | |
584 | }\r | |
585 | \r | |
84b5c78e | 586 | if (!EFI_IP4_EQUAL (&NewConfigData->RemoteAddress, &mZeroIp4Addr) && (NewConfigData->RemotePort != OldConfigData->RemotePort)) {\r |
8a67d61d | 587 | //\r |
588 | // The RemotePort differs if it's designated in the configdata.\r | |
589 | //\r | |
590 | return FALSE;\r | |
591 | }\r | |
592 | \r | |
593 | //\r | |
594 | // All checks pass, return TRUE.\r | |
595 | //\r | |
596 | return TRUE;\r | |
597 | }\r | |
598 | \r | |
599 | \r | |
600 | /**\r | |
601 | This function builds the Ip4 configdata from the Udp4ConfigData.\r | |
602 | \r | |
603 | @param Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.\r | |
604 | @param Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.\r | |
605 | \r | |
606 | @return None.\r | |
607 | \r | |
608 | **/\r | |
609 | VOID\r | |
610 | Udp4BuildIp4ConfigData (\r | |
611 | IN EFI_UDP4_CONFIG_DATA *Udp4ConfigData,\r | |
612 | IN EFI_IP4_CONFIG_DATA *Ip4ConfigData\r | |
613 | )\r | |
614 | {\r | |
687a2e5f | 615 | CopyMem (Ip4ConfigData, &mIpIoDefaultIpConfigData, sizeof (*Ip4ConfigData));\r |
4eb65aff | 616 | \r |
8a67d61d | 617 | Ip4ConfigData->DefaultProtocol = EFI_IP_PROTO_UDP;\r |
618 | Ip4ConfigData->AcceptBroadcast = Udp4ConfigData->AcceptBroadcast;\r | |
619 | Ip4ConfigData->AcceptPromiscuous = Udp4ConfigData->AcceptPromiscuous;\r | |
620 | Ip4ConfigData->UseDefaultAddress = Udp4ConfigData->UseDefaultAddress;\r | |
621 | Ip4ConfigData->StationAddress = Udp4ConfigData->StationAddress;\r | |
622 | Ip4ConfigData->SubnetMask = Udp4ConfigData->SubnetMask;\r | |
623 | \r | |
624 | //\r | |
625 | // use the -1 magic number to disable the receiving process of the ip instance.\r | |
626 | //\r | |
627 | Ip4ConfigData->ReceiveTimeout = (UINT32) (-1);\r | |
628 | }\r | |
629 | \r | |
630 | \r | |
631 | /**\r | |
632 | This function validates the TxToken, it returns the error code according to the spec.\r | |
633 | \r | |
634 | @param Instance Pointer to the udp instance context data.\r | |
635 | @param TxToken Pointer to the token to be checked.\r | |
636 | \r | |
637 | @retval EFI_SUCCESS The TxToken is valid.\r | |
638 | @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is\r | |
639 | NULL. Token is NULL. Token.Event is NULL.\r | |
640 | Token.Packet.TxData is NULL.\r | |
641 | Token.Packet.TxData.FragmentCount is zero.\r | |
642 | Token.Packet.TxData.DataLength is not equal to the\r | |
643 | sum of fragment lengths. One or more of the\r | |
644 | Token.Packet.TxData.FragmentTable[].\r | |
645 | FragmentLength fields is zero. One or more of the\r | |
646 | Token.Packet.TxData.FragmentTable[].\r | |
647 | FragmentBuffer fields is NULL.\r | |
648 | Token.Packet.TxData. GatewayAddress is not a\r | |
649 | unicast IPv4 address if it is not NULL. One or\r | |
650 | more IPv4 addresses in Token.Packet.TxData.\r | |
651 | UdpSessionData are not valid unicast IPv4\r | |
652 | addresses if the UdpSessionData is not NULL.\r | |
653 | @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP\r | |
654 | packet size.\r | |
655 | \r | |
656 | **/\r | |
657 | EFI_STATUS\r | |
658 | Udp4ValidateTxToken (\r | |
659 | IN UDP4_INSTANCE_DATA *Instance,\r | |
660 | IN EFI_UDP4_COMPLETION_TOKEN *TxToken\r | |
661 | )\r | |
662 | {\r | |
663 | EFI_UDP4_TRANSMIT_DATA *TxData;\r | |
664 | UINT32 Index;\r | |
665 | UINT32 TotalLen;\r | |
666 | EFI_UDP4_CONFIG_DATA *ConfigData;\r | |
667 | EFI_UDP4_SESSION_DATA *UdpSessionData;\r | |
668 | IP4_ADDR SourceAddress;\r | |
772db4bb | 669 | IP4_ADDR GatewayAddress;\r |
8a67d61d | 670 | \r |
671 | if (TxToken->Event == NULL) {\r | |
672 | return EFI_INVALID_PARAMETER;\r | |
673 | }\r | |
674 | \r | |
675 | TxData = TxToken->Packet.TxData;\r | |
676 | \r | |
677 | if ((TxData == NULL) || (TxData->FragmentCount == 0)) {\r | |
678 | return EFI_INVALID_PARAMETER;\r | |
679 | }\r | |
680 | \r | |
681 | TotalLen = 0;\r | |
682 | for (Index = 0; Index < TxData->FragmentCount; Index++) {\r | |
683 | \r | |
684 | if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||\r | |
685 | (TxData->FragmentTable[Index].FragmentLength == 0)) {\r | |
686 | //\r | |
687 | // if the FragmentBuffer is NULL or the FragmentLeng is zero.\r | |
688 | //\r | |
689 | return EFI_INVALID_PARAMETER;\r | |
690 | }\r | |
691 | \r | |
692 | TotalLen += TxData->FragmentTable[Index].FragmentLength;\r | |
693 | }\r | |
694 | \r | |
695 | if (TotalLen != TxData->DataLength) {\r | |
696 | //\r | |
697 | // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the\r | |
698 | // DataLength.\r | |
699 | //\r | |
700 | return EFI_INVALID_PARAMETER;\r | |
701 | }\r | |
702 | \r | |
772db4bb | 703 | if (TxData->GatewayAddress != NULL) {\r |
704 | NetCopyMem (&GatewayAddress, TxData->GatewayAddress, sizeof (IP4_ADDR));\r | |
705 | \r | |
706 | if (!Ip4IsUnicast (NTOHL (GatewayAddress), 0)) {\r | |
707 | //\r | |
708 | // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.\r | |
709 | //\r | |
710 | return EFI_INVALID_PARAMETER;\r | |
711 | }\r | |
8a67d61d | 712 | }\r |
713 | \r | |
714 | ConfigData = &Instance->ConfigData;\r | |
715 | UdpSessionData = TxData->UdpSessionData;\r | |
716 | \r | |
717 | if (UdpSessionData != NULL) {\r | |
718 | \r | |
772db4bb | 719 | NetCopyMem (&SourceAddress, &UdpSessionData->SourceAddress, sizeof (IP4_ADDR));\r |
8a67d61d | 720 | \r |
772db4bb | 721 | if ((SourceAddress != 0) && !Ip4IsUnicast (HTONL (SourceAddress), 0)) {\r |
8a67d61d | 722 | //\r |
723 | // Check whether SourceAddress is a valid IPv4 address in case it's not zero.\r | |
724 | // The configured station address is used if SourceAddress is zero.\r | |
725 | //\r | |
726 | return EFI_INVALID_PARAMETER;\r | |
727 | }\r | |
728 | \r | |
729 | if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) {\r | |
730 | //\r | |
731 | // Ambiguous, no avalaible DestinationPort for this token.\r | |
732 | //\r | |
733 | return EFI_INVALID_PARAMETER;\r | |
734 | }\r | |
735 | \r | |
84b5c78e | 736 | if (EFI_IP4_EQUAL (&UdpSessionData->DestinationAddress, &mZeroIp4Addr)) {\r |
8a67d61d | 737 | //\r |
738 | // The DestinationAddress specified in the UdpSessionData is 0.\r | |
739 | //\r | |
740 | return EFI_INVALID_PARAMETER;\r | |
741 | }\r | |
84b5c78e | 742 | } else if (EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &mZeroIp4Addr)) {\r |
8a67d61d | 743 | //\r |
744 | // the configured RemoteAddress is all zero, and the user doens't override the\r | |
745 | // destination address.\r | |
746 | //\r | |
747 | return EFI_INVALID_PARAMETER;\r | |
748 | }\r | |
749 | \r | |
750 | if (TxData->DataLength > UDP4_MAX_DATA_SIZE) {\r | |
751 | return EFI_BAD_BUFFER_SIZE;\r | |
752 | }\r | |
753 | \r | |
754 | return EFI_SUCCESS;\r | |
755 | }\r | |
756 | \r | |
757 | \r | |
758 | /**\r | |
759 | This function checks whether the specified Token duplicates with the one in the Map.\r | |
760 | \r | |
761 | @param Map Pointer to the NET_MAP.\r | |
762 | @param Item Pointer to the NET_MAP_ITEM contain the pointer to\r | |
763 | the Token.\r | |
764 | @param Context Pointer to the Token to be checked.\r | |
765 | \r | |
766 | @retval EFI_SUCCESS The Token specified by Context differs from the\r | |
767 | one in the Item.\r | |
768 | @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.\r | |
769 | \r | |
770 | **/\r | |
771 | EFI_STATUS\r | |
772 | Udp4TokenExist (\r | |
773 | IN NET_MAP *Map,\r | |
774 | IN NET_MAP_ITEM *Item,\r | |
775 | IN VOID *Context\r | |
776 | )\r | |
777 | {\r | |
778 | EFI_UDP4_COMPLETION_TOKEN *Token;\r | |
779 | EFI_UDP4_COMPLETION_TOKEN *TokenInItem;\r | |
780 | \r | |
781 | Token = (EFI_UDP4_COMPLETION_TOKEN*) Context;\r | |
782 | TokenInItem = (EFI_UDP4_COMPLETION_TOKEN*) Item->Key;\r | |
783 | \r | |
784 | if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {\r | |
785 | //\r | |
786 | // The Token duplicates with the TokenInItem in case either the two pointers are the\r | |
787 | // same or the Events of these two tokens are the same.\r | |
788 | //\r | |
789 | return EFI_ACCESS_DENIED;\r | |
790 | }\r | |
791 | \r | |
792 | return EFI_SUCCESS;\r | |
793 | }\r | |
794 | \r | |
795 | \r | |
796 | /**\r | |
797 | This function calculates the checksum for the Packet, utilizing the pre-calculated\r | |
798 | pseudo HeadSum to reduce some overhead.\r | |
799 | \r | |
800 | @param Packet Pointer to the NET_BUF contains the udp datagram.\r | |
801 | @param HeadSum Checksum of the pseudo header execpt the length\r | |
802 | field.\r | |
803 | \r | |
804 | @return The 16-bit checksum of this udp datagram.\r | |
805 | \r | |
806 | **/\r | |
807 | UINT16\r | |
808 | Udp4Checksum (\r | |
809 | IN NET_BUF *Packet,\r | |
810 | IN UINT16 HeadSum\r | |
811 | )\r | |
812 | {\r | |
813 | UINT16 Checksum;\r | |
814 | \r | |
815 | Checksum = NetbufChecksum (Packet);\r | |
816 | Checksum = NetAddChecksum (Checksum, HeadSum);\r | |
817 | \r | |
818 | Checksum = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize));\r | |
819 | \r | |
687a2e5f | 820 | return (UINT16) ~Checksum;\r |
8a67d61d | 821 | }\r |
822 | \r | |
823 | \r | |
824 | /**\r | |
825 | This function removes the specified Token from the TokenMap.\r | |
826 | \r | |
827 | @param TokenMap Pointer to the NET_MAP containing the tokens.\r | |
828 | @param Token Pointer to the Token to be removed.\r | |
829 | \r | |
830 | @retval EFI_SUCCESS The specified Token is removed from the TokenMap.\r | |
831 | @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.\r | |
832 | \r | |
833 | **/\r | |
834 | EFI_STATUS\r | |
835 | Udp4RemoveToken (\r | |
836 | IN NET_MAP *TokenMap,\r | |
837 | IN EFI_UDP4_COMPLETION_TOKEN *Token\r | |
838 | )\r | |
839 | {\r | |
840 | NET_MAP_ITEM *Item;\r | |
841 | \r | |
842 | //\r | |
843 | // Find the Token first.\r | |
844 | //\r | |
845 | Item = NetMapFindKey (TokenMap, (VOID *) Token);\r | |
846 | \r | |
847 | if (Item != NULL) {\r | |
848 | //\r | |
849 | // Remove the token if it's found in the map.\r | |
850 | //\r | |
851 | NetMapRemoveItem (TokenMap, Item, NULL);\r | |
852 | \r | |
853 | return EFI_SUCCESS;\r | |
854 | }\r | |
855 | \r | |
856 | return EFI_NOT_FOUND;\r | |
857 | }\r | |
858 | \r | |
859 | \r | |
860 | /**\r | |
861 | This function is the packet transmitting notify function registered to the IpIo\r | |
862 | interface. It's called to signal the udp TxToken when IpIo layer completes the\r | |
863 | transmitting of the udp datagram.\r | |
864 | \r | |
865 | @param Status The completion status of the output udp datagram.\r | |
866 | @param Context Pointer to the context data.\r | |
867 | @param Sender Pointer to the Ip sender of the udp datagram.\r | |
868 | @param NotifyData Pointer to the notify data.\r | |
869 | \r | |
870 | @return None.\r | |
871 | \r | |
872 | **/\r | |
873 | STATIC\r | |
874 | VOID\r | |
875 | Udp4DgramSent (\r | |
876 | IN EFI_STATUS Status,\r | |
877 | IN VOID *Context,\r | |
878 | IN VOID *Sender,\r | |
879 | IN VOID *NotifyData\r | |
880 | )\r | |
881 | {\r | |
882 | UDP4_INSTANCE_DATA *Instance;\r | |
883 | EFI_UDP4_COMPLETION_TOKEN *Token;\r | |
884 | \r | |
885 | Instance = (UDP4_INSTANCE_DATA *) Context;\r | |
886 | Token = (EFI_UDP4_COMPLETION_TOKEN *) NotifyData;\r | |
887 | \r | |
888 | if (Udp4RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) {\r | |
889 | //\r | |
890 | // The token may be cancelled. Only signal it if the remove operation succeeds.\r | |
891 | //\r | |
892 | Token->Status = Status;\r | |
893 | gBS->SignalEvent (Token->Event);\r | |
894 | }\r | |
895 | }\r | |
896 | \r | |
897 | \r | |
898 | /**\r | |
899 | This function processes the received datagram passed up by the IpIo layer.\r | |
900 | \r | |
901 | @param Status The status of this udp datagram.\r | |
902 | @param IcmpError The IcmpError code, only available when Status is\r | |
903 | EFI_ICMP_ERROR.\r | |
904 | @param NetSession Pointer to the EFI_NET_SESSION_DATA.\r | |
905 | @param Packet Pointer to the NET_BUF containing the received udp\r | |
906 | datagram.\r | |
907 | @param Context Pointer to the context data.\r | |
908 | \r | |
909 | @return None.\r | |
910 | \r | |
911 | **/\r | |
912 | STATIC\r | |
913 | VOID\r | |
914 | Udp4DgramRcvd (\r | |
915 | IN EFI_STATUS Status,\r | |
916 | IN ICMP_ERROR IcmpError,\r | |
917 | IN EFI_NET_SESSION_DATA *NetSession,\r | |
918 | IN NET_BUF *Packet,\r | |
919 | IN VOID *Context\r | |
920 | )\r | |
921 | {\r | |
922 | NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE);\r | |
923 | \r | |
924 | //\r | |
925 | // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.\r | |
926 | //\r | |
927 | if (Status == EFI_SUCCESS) {\r | |
928 | //\r | |
929 | // Demultiplex the received datagram.\r | |
930 | //\r | |
931 | Udp4Demultiplex ((UDP4_SERVICE_DATA *) Context, NetSession, Packet);\r | |
932 | } else {\r | |
933 | //\r | |
934 | // Handle the ICMP_ERROR packet.\r | |
935 | //\r | |
936 | Udp4IcmpHandler ((UDP4_SERVICE_DATA *) Context, IcmpError, NetSession, Packet);\r | |
937 | }\r | |
938 | }\r | |
939 | \r | |
940 | \r | |
941 | /**\r | |
942 | This function removes the multicast group specified by Arg from the Map.\r | |
943 | \r | |
944 | @param Map Pointer to the NET_MAP.\r | |
945 | @param Item Pointer to the NET_MAP_ITEM.\r | |
946 | @param Arg Pointer to the Arg, it's the pointer to a\r | |
947 | multicast IPv4 Address.\r | |
948 | \r | |
949 | @retval EFI_SUCCESS The multicast address is removed.\r | |
950 | @retval EFI_ABORTED The specified multicast address is removed and the\r | |
951 | Arg is not NULL.\r | |
952 | \r | |
953 | **/\r | |
954 | EFI_STATUS\r | |
955 | Udp4LeaveGroup (\r | |
956 | IN NET_MAP *Map,\r | |
957 | IN NET_MAP_ITEM *Item,\r | |
958 | IN VOID *Arg OPTIONAL\r | |
959 | )\r | |
960 | {\r | |
961 | EFI_IPv4_ADDRESS *McastIp;\r | |
962 | \r | |
963 | McastIp = Arg;\r | |
964 | \r | |
84b5c78e | 965 | if ((McastIp != NULL) && (!EFI_IP4_EQUAL (McastIp, &(Item->Key)))) {\r |
8a67d61d | 966 | //\r |
687a2e5f | 967 | // McastIp is not NULL and the multicast address contained in the Item\r |
8a67d61d | 968 | // is not the same as McastIp.\r |
969 | //\r | |
970 | return EFI_SUCCESS;\r | |
971 | }\r | |
972 | \r | |
973 | //\r | |
974 | // Remove this Item.\r | |
975 | //\r | |
976 | NetMapRemoveItem (Map, Item, NULL);\r | |
977 | \r | |
978 | if (McastIp != NULL) {\r | |
979 | //\r | |
980 | // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.\r | |
981 | //\r | |
982 | return EFI_ABORTED;\r | |
983 | }\r | |
984 | \r | |
985 | return EFI_SUCCESS;\r | |
986 | }\r | |
987 | \r | |
988 | \r | |
989 | /**\r | |
990 | This function cancle the token specified by Arg in the Map.\r | |
991 | \r | |
992 | @param Map Pointer to the NET_MAP.\r | |
993 | @param Item Pointer to the NET_MAP_ITEM.\r | |
994 | @param Arg Pointer to the token to be cancelled, if NULL, all\r | |
995 | the tokens in this Map will be cancelled.\r | |
996 | \r | |
997 | @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token\r | |
998 | is not the same as that in the Item if Arg is not\r | |
999 | NULL.\r | |
1000 | @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is\r | |
1001 | cancelled.\r | |
1002 | \r | |
1003 | **/\r | |
1004 | STATIC\r | |
1005 | EFI_STATUS\r | |
1006 | Udp4CancelTokens (\r | |
1007 | IN NET_MAP *Map,\r | |
1008 | IN NET_MAP_ITEM *Item,\r | |
1009 | IN VOID *Arg OPTIONAL\r | |
1010 | )\r | |
1011 | {\r | |
1012 | EFI_UDP4_COMPLETION_TOKEN *TokenToCancel;\r | |
1013 | NET_BUF *Packet;\r | |
1014 | IP_IO *IpIo;\r | |
1015 | \r | |
1016 | if ((Arg != NULL) && (Item->Key != Arg)) {\r | |
1017 | return EFI_SUCCESS;\r | |
1018 | }\r | |
1019 | \r | |
1020 | if (Item->Value != NULL) {\r | |
1021 | //\r | |
1022 | // If the token is a transmit token, the corresponding Packet is recorded in\r | |
1023 | // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken\r | |
1024 | // will invoke Udp4DgramSent, the token will be signaled and this Item will\r | |
1025 | // be removed from the Map there.\r | |
1026 | //\r | |
1027 | Packet = (NET_BUF *) (Item->Value);\r | |
1028 | IpIo = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0]));\r | |
1029 | \r | |
1030 | IpIoCancelTxToken (IpIo, Packet);\r | |
1031 | } else {\r | |
1032 | //\r | |
1033 | // The token is a receive token. Abort it and remove it from the Map.\r | |
1034 | //\r | |
1035 | TokenToCancel = (EFI_UDP4_COMPLETION_TOKEN *) Item->Key;\r | |
1036 | \r | |
1037 | TokenToCancel->Status = EFI_ABORTED;\r | |
1038 | gBS->SignalEvent (TokenToCancel->Event);\r | |
1039 | \r | |
1040 | NetMapRemoveItem (Map, Item, NULL);\r | |
1041 | }\r | |
1042 | \r | |
1043 | if (Arg != NULL) {\r | |
1044 | return EFI_ABORTED;\r | |
1045 | }\r | |
1046 | \r | |
1047 | return EFI_SUCCESS;\r | |
1048 | }\r | |
1049 | \r | |
1050 | \r | |
1051 | /**\r | |
1052 | This function removes all the Wrap datas in the RcvdDgramQue.\r | |
1053 | \r | |
1054 | @param RcvdDgramQue Pointer to the list containing all the Wrap datas.\r | |
1055 | \r | |
1056 | @return None.\r | |
1057 | \r | |
1058 | **/\r | |
1059 | VOID\r | |
1060 | Udp4FlushRxData (\r | |
1061 | IN NET_LIST_ENTRY *RcvdDgramQue\r | |
1062 | )\r | |
1063 | {\r | |
1064 | UDP4_RXDATA_WRAP *Wrap;\r | |
1065 | EFI_TPL OldTpl;\r | |
1066 | \r | |
1067 | OldTpl = NET_RAISE_TPL (NET_TPL_RECYCLE);\r | |
1068 | \r | |
1069 | while (!NetListIsEmpty (RcvdDgramQue)) {\r | |
1070 | //\r | |
1071 | // Iterate all the Wraps in the RcvdDgramQue.\r | |
1072 | //\r | |
1073 | Wrap = NET_LIST_HEAD (RcvdDgramQue, UDP4_RXDATA_WRAP, Link);\r | |
1074 | \r | |
1075 | //\r | |
1076 | // The Wrap will be removed from the RcvdDgramQue by this function call.\r | |
1077 | //\r | |
1078 | Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap);\r | |
1079 | }\r | |
1080 | \r | |
1081 | NET_RESTORE_TPL (OldTpl);\r | |
1082 | }\r | |
1083 | \r | |
1084 | \r | |
1085 | \r | |
1086 | /**\r | |
1087 | \r | |
1088 | @param Instance Pointer to the udp instance context data.\r | |
1089 | @param Token Pointer to the token to be canceled, if NULL, all\r | |
1090 | tokens in this instance will be cancelled.\r | |
1091 | \r | |
1092 | @retval EFI_SUCCESS The Token is cancelled.\r | |
1093 | @retval EFI_NOT_FOUND The Token is not found.\r | |
1094 | \r | |
1095 | **/\r | |
1096 | EFI_STATUS\r | |
1097 | Udp4InstanceCancelToken (\r | |
1098 | IN UDP4_INSTANCE_DATA *Instance,\r | |
1099 | IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL\r | |
1100 | )\r | |
1101 | {\r | |
1102 | EFI_STATUS Status;\r | |
1103 | \r | |
1104 | //\r | |
1105 | // Cancle this token from the TxTokens map.\r | |
1106 | //\r | |
1107 | Status = NetMapIterate (&Instance->TxTokens, Udp4CancelTokens, Token);\r | |
1108 | \r | |
1109 | if ((Token != NULL) && (Status == EFI_ABORTED)) {\r | |
1110 | //\r | |
1111 | // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from\r | |
1112 | // the TxTokens, just return success.\r | |
1113 | //\r | |
1114 | return EFI_SUCCESS;\r | |
1115 | }\r | |
1116 | \r | |
1117 | //\r | |
1118 | // Try to cancel this token from the RxTokens map in condition either the Token\r | |
1119 | // is NULL or the specified Token is not in TxTokens.\r | |
1120 | //\r | |
1121 | Status = NetMapIterate (&Instance->RxTokens, Udp4CancelTokens, Token);\r | |
1122 | \r | |
1123 | if ((Token != NULL) && (Status == EFI_SUCCESS)) {\r | |
1124 | //\r | |
1125 | // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the\r | |
1126 | // TxTokens nor the RxTokens, or say, it's not found.\r | |
1127 | //\r | |
1128 | return EFI_NOT_FOUND;\r | |
1129 | }\r | |
1130 | \r | |
1131 | ASSERT ((Token != NULL) || ((0 == NetMapGetCount (&Instance->TxTokens))\r | |
1132 | && (0 == NetMapGetCount (&Instance->RxTokens))));\r | |
1133 | \r | |
1134 | return EFI_SUCCESS;\r | |
1135 | }\r | |
1136 | \r | |
1137 | \r | |
1138 | /**\r | |
1139 | This function matches the received udp datagram with the Instance.\r | |
1140 | \r | |
1141 | @param Instance Pointer to the udp instance context data.\r | |
1142 | @param Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted\r | |
1143 | from the received udp datagram.\r | |
1144 | \r | |
1145 | @return The udp datagram matches the receiving requirments of the Instance or not.\r | |
1146 | \r | |
1147 | **/\r | |
1148 | STATIC\r | |
1149 | BOOLEAN\r | |
1150 | Udp4MatchDgram (\r | |
1151 | IN UDP4_INSTANCE_DATA *Instance,\r | |
1152 | IN EFI_UDP4_SESSION_DATA *Udp4Session\r | |
1153 | )\r | |
1154 | {\r | |
1155 | EFI_UDP4_CONFIG_DATA *ConfigData;\r | |
1156 | IP4_ADDR Destination;\r | |
1157 | \r | |
1158 | ConfigData = &Instance->ConfigData;\r | |
1159 | \r | |
1160 | if (ConfigData->AcceptPromiscuous) {\r | |
1161 | //\r | |
1162 | // Always matches if this instance is in the promiscuous state.\r | |
1163 | //\r | |
1164 | return TRUE;\r | |
1165 | }\r | |
1166 | \r | |
1167 | if ((!ConfigData->AcceptAnyPort && (Udp4Session->DestinationPort != ConfigData->StationPort)) ||\r | |
1168 | ((ConfigData->RemotePort != 0) && (Udp4Session->SourcePort != ConfigData->RemotePort))) {\r | |
1169 | //\r | |
1170 | // The local port or the remote port doesn't match.\r | |
1171 | //\r | |
1172 | return FALSE;\r | |
1173 | }\r | |
1174 | \r | |
84b5c78e | 1175 | if (!EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &mZeroIp4Addr) &&\r |
1176 | !EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &Udp4Session->SourceAddress)) {\r | |
8a67d61d | 1177 | //\r |
1178 | // This datagram doesn't come from the instance's specified sender.\r | |
1179 | //\r | |
1180 | return FALSE;\r | |
1181 | }\r | |
1182 | \r | |
84b5c78e | 1183 | if (EFI_IP4_EQUAL (&ConfigData->StationAddress, &mZeroIp4Addr) ||\r |
1184 | EFI_IP4_EQUAL (&Udp4Session->DestinationAddress, &ConfigData->StationAddress)) {\r | |
8a67d61d | 1185 | //\r |
1186 | // The instance is configured to receive datagrams destinated to any station IP or\r | |
1187 | // the destination address of this datagram matches the configured station IP.\r | |
1188 | //\r | |
1189 | return TRUE;\r | |
1190 | }\r | |
1191 | \r | |
772db4bb | 1192 | NetCopyMem (&Destination, &Udp4Session->DestinationAddress, sizeof (IP4_ADDR));\r |
8a67d61d | 1193 | \r |
1194 | if (IP4_IS_LOCAL_BROADCAST (Destination) && ConfigData->AcceptBroadcast) {\r | |
1195 | //\r | |
1196 | // The instance is configured to receive broadcast and this is a broadcast packet.\r | |
1197 | //\r | |
1198 | return TRUE;\r | |
1199 | }\r | |
1200 | \r | |
1201 | if (IP4_IS_MULTICAST (NTOHL (Destination)) &&\r | |
1202 | (NULL != NetMapFindKey (&Instance->McastIps, (VOID *) (UINTN) Destination))) {\r | |
1203 | //\r | |
1204 | // It's a multicast packet and the multicast address is accepted by this instance.\r | |
1205 | //\r | |
1206 | return TRUE;\r | |
1207 | }\r | |
1208 | \r | |
1209 | return FALSE;\r | |
1210 | }\r | |
1211 | \r | |
1212 | \r | |
1213 | /**\r | |
1214 | This function removes the Wrap specified by Context and release relevant resources.\r | |
1215 | \r | |
1216 | @param Event The Event this notify function registered to.\r | |
1217 | @param Context Pointer to the context data.\r | |
1218 | \r | |
1219 | @return None.\r | |
1220 | \r | |
1221 | **/\r | |
1222 | STATIC\r | |
1223 | VOID\r | |
1224 | EFIAPI\r | |
1225 | Udp4RecycleRxDataWrap (\r | |
1226 | IN EFI_EVENT Event,\r | |
1227 | IN VOID *Context\r | |
1228 | )\r | |
1229 | {\r | |
1230 | UDP4_RXDATA_WRAP *Wrap;\r | |
1231 | \r | |
1232 | Wrap = (UDP4_RXDATA_WRAP *) Context;\r | |
1233 | \r | |
1234 | //\r | |
1235 | // Remove the Wrap from the list it belongs to.\r | |
1236 | //\r | |
1237 | NetListRemoveEntry (&Wrap->Link);\r | |
1238 | \r | |
1239 | //\r | |
1240 | // Free the Packet associated with this Wrap.\r | |
1241 | //\r | |
1242 | NetbufFree (Wrap->Packet);\r | |
1243 | \r | |
1244 | //\r | |
1245 | // Close the event.\r | |
1246 | //\r | |
1247 | gBS->CloseEvent (Wrap->RxData.RecycleSignal);\r | |
1248 | \r | |
1249 | NetFreePool (Wrap);\r | |
1250 | }\r | |
1251 | \r | |
1252 | \r | |
1253 | /**\r | |
1254 | This function wraps the Packet and the RxData.\r | |
1255 | \r | |
1256 | @param Instance Pointer to the instance context data.\r | |
1257 | @param Packet Pointer to the buffer containing the received\r | |
1258 | datagram.\r | |
1259 | @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this\r | |
1260 | datagram.\r | |
1261 | \r | |
1262 | @return Pointer to the structure wrapping the RxData and the Packet.\r | |
1263 | \r | |
1264 | **/\r | |
1265 | STATIC\r | |
1266 | UDP4_RXDATA_WRAP *\r | |
1267 | Udp4WrapRxData (\r | |
1268 | IN UDP4_INSTANCE_DATA *Instance,\r | |
1269 | IN NET_BUF *Packet,\r | |
1270 | IN EFI_UDP4_RECEIVE_DATA *RxData\r | |
1271 | )\r | |
1272 | {\r | |
1273 | EFI_STATUS Status;\r | |
1274 | UDP4_RXDATA_WRAP *Wrap;\r | |
1275 | \r | |
1276 | //\r | |
1277 | // Allocate buffer for the Wrap.\r | |
1278 | //\r | |
1279 | Wrap = NetAllocatePool (sizeof (UDP4_RXDATA_WRAP) +\r | |
1280 | (Packet->BlockOpNum - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));\r | |
1281 | if (Wrap == NULL) {\r | |
1282 | return NULL;\r | |
1283 | }\r | |
1284 | \r | |
1285 | NetListInit (&Wrap->Link);\r | |
1286 | \r | |
687a2e5f | 1287 | CopyMem (&Wrap->RxData, RxData, sizeof (Wrap->RxData));\r |
8a67d61d | 1288 | \r |
1289 | //\r | |
1290 | // Create the Recycle event.\r | |
1291 | //\r | |
1292 | Status = gBS->CreateEvent (\r | |
1293 | EVT_NOTIFY_SIGNAL,\r | |
1294 | NET_TPL_RECYCLE,\r | |
1295 | Udp4RecycleRxDataWrap,\r | |
1296 | Wrap,\r | |
1297 | &Wrap->RxData.RecycleSignal\r | |
1298 | );\r | |
1299 | if (EFI_ERROR (Status)) {\r | |
1300 | NetFreePool (Wrap);\r | |
1301 | return NULL;\r | |
1302 | }\r | |
1303 | \r | |
1304 | Wrap->Packet = Packet;\r | |
1305 | Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout;\r | |
1306 | \r | |
1307 | return Wrap;\r | |
1308 | }\r | |
1309 | \r | |
1310 | \r | |
1311 | /**\r | |
1312 | This function enqueues the received datagram into the instances' receiving queues.\r | |
1313 | \r | |
1314 | @param Udp4Service Pointer to the udp service context data.\r | |
1315 | @param Packet Pointer to the buffer containing the received\r | |
1316 | datagram.\r | |
1317 | @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this\r | |
1318 | datagram.\r | |
1319 | \r | |
1320 | @return The times this datagram is enqueued.\r | |
1321 | \r | |
1322 | **/\r | |
1323 | STATIC\r | |
1324 | UINTN\r | |
1325 | Udp4EnqueueDgram (\r | |
1326 | IN UDP4_SERVICE_DATA *Udp4Service,\r | |
1327 | IN NET_BUF *Packet,\r | |
1328 | IN EFI_UDP4_RECEIVE_DATA *RxData\r | |
1329 | )\r | |
1330 | {\r | |
1331 | NET_LIST_ENTRY *Entry;\r | |
1332 | UDP4_INSTANCE_DATA *Instance;\r | |
1333 | UDP4_RXDATA_WRAP *Wrap;\r | |
1334 | UINTN Enqueued;\r | |
1335 | \r | |
1336 | Enqueued = 0;\r | |
1337 | \r | |
1338 | NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r | |
1339 | //\r | |
1340 | // Iterate the instances.\r | |
1341 | //\r | |
1342 | Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);\r | |
1343 | \r | |
1344 | if (!Instance->Configured) {\r | |
1345 | continue;\r | |
1346 | }\r | |
1347 | \r | |
1348 | if (Udp4MatchDgram (Instance, &RxData->UdpSession)) {\r | |
1349 | //\r | |
1350 | // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.\r | |
1351 | //\r | |
e5e12de7 | 1352 | Wrap = Udp4WrapRxData (Instance, Packet, RxData);\r |
8a67d61d | 1353 | if (Wrap == NULL) {\r |
1354 | continue;\r | |
1355 | }\r | |
1356 | \r | |
1357 | NET_GET_REF (Packet);\r | |
1358 | \r | |
1359 | NetListInsertTail (&Instance->RcvdDgramQue, &Wrap->Link);\r | |
1360 | \r | |
1361 | Enqueued++;\r | |
1362 | }\r | |
1363 | }\r | |
1364 | \r | |
1365 | return Enqueued;\r | |
1366 | }\r | |
1367 | \r | |
1368 | \r | |
1369 | /**\r | |
1370 | This function delivers the received datagrams for the specified instance.\r | |
1371 | \r | |
1372 | @param Instance Pointer to the instance context data.\r | |
1373 | \r | |
1374 | @return None.\r | |
1375 | \r | |
1376 | **/\r | |
1377 | VOID\r | |
1378 | Udp4InstanceDeliverDgram (\r | |
1379 | IN UDP4_INSTANCE_DATA *Instance\r | |
1380 | )\r | |
1381 | {\r | |
1382 | UDP4_RXDATA_WRAP *Wrap;\r | |
1383 | EFI_UDP4_COMPLETION_TOKEN *Token;\r | |
1384 | NET_BUF *Dup;\r | |
1385 | EFI_UDP4_RECEIVE_DATA *RxData;\r | |
1386 | \r | |
1387 | if (!NetListIsEmpty (&Instance->RcvdDgramQue) &&\r | |
1388 | !NetMapIsEmpty (&Instance->RxTokens)) {\r | |
1389 | \r | |
1390 | Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link);\r | |
1391 | \r | |
1392 | if (NET_BUF_SHARED (Wrap->Packet)) {\r | |
1393 | //\r | |
1394 | // Duplicate the Packet if it is shared between instances.\r | |
1395 | //\r | |
1396 | Dup = NetbufDuplicate (Wrap->Packet, NULL, 0);\r | |
1397 | if (Dup == NULL) {\r | |
1398 | return;\r | |
1399 | }\r | |
1400 | \r | |
1401 | NetbufFree (Wrap->Packet);\r | |
1402 | \r | |
1403 | Wrap->Packet = Dup;\r | |
1404 | }\r | |
1405 | \r | |
1406 | NetListRemoveHead (&Instance->RcvdDgramQue);\r | |
1407 | \r | |
1408 | Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);\r | |
1409 | \r | |
1410 | //\r | |
1411 | // Build the FragmentTable and set the FragmentCount in RxData.\r | |
1412 | //\r | |
1413 | RxData = &Wrap->RxData;\r | |
1414 | RxData->FragmentCount = Wrap->Packet->BlockOpNum;\r | |
1415 | \r | |
1416 | NetbufBuildExt (\r | |
1417 | Wrap->Packet,\r | |
1418 | (NET_FRAGMENT *) RxData->FragmentTable,\r | |
1419 | &RxData->FragmentCount\r | |
1420 | );\r | |
1421 | \r | |
1422 | Token->Status = EFI_SUCCESS;\r | |
1423 | Token->Packet.RxData = &Wrap->RxData;\r | |
1424 | \r | |
1425 | gBS->SignalEvent (Token->Event);\r | |
1426 | \r | |
1427 | NetListInsertTail (&Instance->DeliveredDgramQue, &Wrap->Link);\r | |
1428 | }\r | |
1429 | }\r | |
1430 | \r | |
1431 | \r | |
1432 | /**\r | |
1433 | This function delivers the datagrams enqueued in the instances.\r | |
1434 | \r | |
1435 | @param Udp4Service Pointer to the udp service context data.\r | |
1436 | \r | |
1437 | @return None.\r | |
1438 | \r | |
1439 | **/\r | |
1440 | STATIC\r | |
1441 | VOID\r | |
1442 | Udp4DeliverDgram (\r | |
1443 | IN UDP4_SERVICE_DATA *Udp4Service\r | |
1444 | )\r | |
1445 | {\r | |
1446 | NET_LIST_ENTRY *Entry;\r | |
1447 | UDP4_INSTANCE_DATA *Instance;\r | |
1448 | \r | |
1449 | NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r | |
1450 | //\r | |
1451 | // Iterate the instances.\r | |
1452 | //\r | |
1453 | Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);\r | |
1454 | \r | |
1455 | if (!Instance->Configured) {\r | |
1456 | continue;\r | |
1457 | }\r | |
1458 | \r | |
1459 | //\r | |
1460 | // Deliver the datagrams of this instance.\r | |
1461 | //\r | |
1462 | Udp4InstanceDeliverDgram (Instance);\r | |
1463 | }\r | |
1464 | }\r | |
1465 | \r | |
1466 | \r | |
1467 | /**\r | |
1468 | This function demultiplexes the received udp datagram to the apropriate instances.\r | |
1469 | \r | |
1470 | @param Udp4Service Pointer to the udp service context data.\r | |
1471 | @param NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from\r | |
1472 | the received datagram.\r | |
1473 | @param Packet Pointer to the buffer containing the received udp\r | |
1474 | datagram.\r | |
1475 | \r | |
1476 | @return None.\r | |
1477 | \r | |
1478 | **/\r | |
1479 | STATIC\r | |
1480 | VOID\r | |
1481 | Udp4Demultiplex (\r | |
1482 | IN UDP4_SERVICE_DATA *Udp4Service,\r | |
1483 | IN EFI_NET_SESSION_DATA *NetSession,\r | |
1484 | IN NET_BUF *Packet\r | |
1485 | )\r | |
1486 | {\r | |
1487 | EFI_UDP4_HEADER *Udp4Header;\r | |
1488 | UINT16 HeadSum;\r | |
1489 | EFI_UDP4_RECEIVE_DATA RxData;\r | |
1490 | EFI_UDP4_SESSION_DATA *Udp4Session;\r | |
1491 | UINTN Enqueued;\r | |
1492 | \r | |
1493 | //\r | |
1494 | // Get the datagram header from the packet buffer.\r | |
1495 | //\r | |
1496 | Udp4Header = (EFI_UDP4_HEADER *) NetbufGetByte (Packet, 0, NULL);\r | |
1497 | \r | |
1498 | if (Udp4Header->Checksum != 0) {\r | |
1499 | //\r | |
1500 | // check the checksum.\r | |
1501 | //\r | |
1502 | HeadSum = NetPseudoHeadChecksum (\r | |
1503 | NetSession->Source,\r | |
1504 | NetSession->Dest,\r | |
1505 | EFI_IP_PROTO_UDP,\r | |
1506 | 0\r | |
1507 | );\r | |
1508 | \r | |
1509 | if (Udp4Checksum (Packet, HeadSum) != 0) {\r | |
1510 | //\r | |
1511 | // Wrong checksum.\r | |
1512 | //\r | |
1513 | return;\r | |
1514 | }\r | |
1515 | }\r | |
1516 | \r | |
1517 | gRT->GetTime (&RxData.TimeStamp, NULL);\r | |
1518 | \r | |
772db4bb | 1519 | Udp4Session = &RxData.UdpSession;\r |
1520 | Udp4Session->SourcePort = NTOHS (Udp4Header->SrcPort);\r | |
1521 | Udp4Session->DestinationPort = NTOHS (Udp4Header->DstPort);\r | |
1522 | \r | |
1523 | NetCopyMem (&Udp4Session->SourceAddress, &NetSession->Source, sizeof (EFI_IPv4_ADDRESS));\r | |
1524 | NetCopyMem (&Udp4Session->DestinationAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS));\r | |
8a67d61d | 1525 | \r |
1526 | //\r | |
1527 | // Trim the UDP header.\r | |
1528 | //\r | |
1529 | NetbufTrim (Packet, UDP4_HEADER_SIZE, TRUE);\r | |
1530 | \r | |
1531 | RxData.DataLength = (UINT32) Packet->TotalSize;\r | |
1532 | \r | |
1533 | //\r | |
1534 | // Try to enqueue this datagram into the instances.\r | |
1535 | //\r | |
1536 | Enqueued = Udp4EnqueueDgram (Udp4Service, Packet, &RxData);\r | |
1537 | \r | |
1538 | if (Enqueued == 0) {\r | |
1539 | //\r | |
1540 | // Send the port unreachable ICMP packet before we free this NET_BUF\r | |
1541 | //\r | |
1542 | Udp4SendPortUnreach (Udp4Service->IpIo, NetSession, Udp4Header);\r | |
1543 | }\r | |
1544 | \r | |
1545 | //\r | |
1546 | // Try to free the packet before deliver it.\r | |
1547 | //\r | |
1548 | NetbufFree (Packet);\r | |
1549 | \r | |
1550 | if (Enqueued > 0) {\r | |
1551 | //\r | |
1552 | // Deliver the datagram.\r | |
1553 | //\r | |
1554 | Udp4DeliverDgram (Udp4Service);\r | |
1555 | }\r | |
1556 | }\r | |
1557 | \r | |
1558 | \r | |
1559 | /**\r | |
1560 | This function builds and sends out a icmp port unreachable message.\r | |
1561 | \r | |
1562 | @param IpIo Pointer to the IP_IO instance.\r | |
1563 | @param NetSession Pointer to the EFI_NET_SESSION_DATA of the packet\r | |
1564 | causes this icmp error message.\r | |
1565 | @param Udp4Header Pointer to the udp header of the datagram causes\r | |
1566 | this icmp error message.\r | |
1567 | \r | |
1568 | @return None.\r | |
1569 | \r | |
1570 | **/\r | |
1571 | STATIC\r | |
1572 | VOID\r | |
1573 | Udp4SendPortUnreach (\r | |
1574 | IN IP_IO *IpIo,\r | |
1575 | IN EFI_NET_SESSION_DATA *NetSession,\r | |
1576 | IN VOID *Udp4Header\r | |
1577 | )\r | |
1578 | {\r | |
1579 | NET_BUF *Packet;\r | |
1580 | UINT32 Len;\r | |
1581 | IP4_ICMP_ERROR_HEAD *IcmpErrHdr;\r | |
1582 | EFI_IP4_HEADER *IpHdr;\r | |
1583 | UINT8 *Ptr;\r | |
1584 | IP_IO_OVERRIDE Override;\r | |
1585 | IP_IO_IP_INFO *IpSender;\r | |
1586 | \r | |
1587 | IpSender = IpIoFindSender (&IpIo, NetSession->Dest);\r | |
1588 | if (IpSender == NULL) {\r | |
1589 | //\r | |
1590 | // No apropriate sender, since we cannot send out the ICMP message through\r | |
1591 | // the default zero station address IP instance, abort.\r | |
1592 | //\r | |
1593 | return;\r | |
1594 | }\r | |
1595 | \r | |
1596 | IpHdr = NetSession->IpHdr;\r | |
1597 | \r | |
1598 | //\r | |
1599 | // Calculate the requried length of the icmp error message.\r | |
1600 | //\r | |
1601 | Len = sizeof (IP4_ICMP_ERROR_HEAD) + (EFI_IP4_HEADER_LEN (IpHdr) -\r | |
1602 | sizeof (IP4_HEAD)) + ICMP_ERROR_PACKET_LENGTH;\r | |
1603 | \r | |
1604 | //\r | |
1605 | // Allocate buffer for the icmp error message.\r | |
1606 | //\r | |
1607 | Packet = NetbufAlloc (Len);\r | |
1608 | if (Packet == NULL) {\r | |
1609 | return;\r | |
1610 | }\r | |
1611 | \r | |
1612 | //\r | |
1613 | // Allocate space for the IP4_ICMP_ERROR_HEAD.\r | |
1614 | //\r | |
1615 | IcmpErrHdr = (IP4_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE);\r | |
1616 | \r | |
1617 | //\r | |
1618 | // Set the required fields for the icmp port unreachable message.\r | |
1619 | //\r | |
1620 | IcmpErrHdr->Head.Type = ICMP_TYPE_UNREACH;\r | |
1621 | IcmpErrHdr->Head.Code = ICMP_CODE_UNREACH_PORT;\r | |
1622 | IcmpErrHdr->Head.Checksum = 0;\r | |
1623 | IcmpErrHdr->Fourth = 0;\r | |
1624 | \r | |
1625 | //\r | |
1626 | // Copy the IP header of the datagram tragged the error.\r | |
1627 | //\r | |
1628 | NetCopyMem (&IcmpErrHdr->IpHead, IpHdr, EFI_IP4_HEADER_LEN (IpHdr));\r | |
1629 | \r | |
1630 | //\r | |
1631 | // Copy the UDP header.\r | |
1632 | //\r | |
1633 | Ptr = (UINT8 *) &IcmpErrHdr->IpHead + EFI_IP4_HEADER_LEN (IpHdr);\r | |
1634 | NetCopyMem (Ptr, Udp4Header, ICMP_ERROR_PACKET_LENGTH);\r | |
1635 | \r | |
1636 | //\r | |
1637 | // Calculate the checksum.\r | |
1638 | //\r | |
687a2e5f | 1639 | IcmpErrHdr->Head.Checksum = (UINT16) ~(NetbufChecksum (Packet));\r |
8a67d61d | 1640 | \r |
1641 | //\r | |
1642 | // Fill the override data.\r | |
1643 | //\r | |
772db4bb | 1644 | Override.DoNotFragment = FALSE;\r |
1645 | Override.TypeOfService = 0;\r | |
1646 | Override.TimeToLive = 255;\r | |
1647 | Override.Protocol = EFI_IP_PROTO_ICMP;\r | |
1648 | \r | |
1649 | NetCopyMem (&Override.SourceAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS));\r | |
1650 | NetZeroMem (&Override.GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r | |
8a67d61d | 1651 | \r |
1652 | //\r | |
1653 | // Send out this icmp packet.\r | |
1654 | //\r | |
1655 | IpIoSend (IpIo, Packet, IpSender, NULL, NULL, NetSession->Source, &Override);\r | |
1656 | \r | |
1657 | NetbufFree (Packet);\r | |
1658 | }\r | |
1659 | \r | |
1660 | \r | |
1661 | /**\r | |
1662 | This function handles the received Icmp Error message and demultiplexes it to the\r | |
1663 | instance.\r | |
1664 | \r | |
1665 | @param Udp4Service Pointer to the udp service context data.\r | |
1666 | @param IcmpError The icmp error code.\r | |
1667 | @param NetSession Pointer to the EFI_NET_SESSION_DATA abstracted\r | |
1668 | from the received Icmp Error packet.\r | |
1669 | @param Packet Pointer to the Icmp Error packet.\r | |
1670 | \r | |
1671 | @return None.\r | |
1672 | \r | |
1673 | **/\r | |
1674 | STATIC\r | |
1675 | VOID\r | |
1676 | Udp4IcmpHandler (\r | |
1677 | IN UDP4_SERVICE_DATA *Udp4Service,\r | |
1678 | IN ICMP_ERROR IcmpError,\r | |
1679 | IN EFI_NET_SESSION_DATA *NetSession,\r | |
1680 | IN NET_BUF *Packet\r | |
1681 | )\r | |
1682 | {\r | |
1683 | EFI_UDP4_HEADER *Udp4Header;\r | |
1684 | EFI_UDP4_SESSION_DATA Udp4Session;\r | |
1685 | NET_LIST_ENTRY *Entry;\r | |
1686 | UDP4_INSTANCE_DATA *Instance;\r | |
1687 | \r | |
1688 | Udp4Header = (EFI_UDP4_HEADER *) NetbufGetByte (Packet, 0, NULL);\r | |
1689 | \r | |
772db4bb | 1690 | NetCopyMem (&Udp4Session.SourceAddress, &NetSession->Source, sizeof (EFI_IPv4_ADDRESS));\r |
1691 | NetCopyMem (&Udp4Session.DestinationAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS));\r | |
1692 | \r | |
1693 | Udp4Session.SourcePort = NTOHS (Udp4Header->DstPort);\r | |
1694 | Udp4Session.DestinationPort = NTOHS (Udp4Header->SrcPort);\r | |
8a67d61d | 1695 | \r |
1696 | NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r | |
1697 | //\r | |
1698 | // Iterate all the instances.\r | |
1699 | //\r | |
1700 | Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);\r | |
1701 | \r | |
1702 | if (!Instance->Configured ||\r | |
1703 | Instance->ConfigData.AcceptPromiscuous ||\r | |
1704 | Instance->ConfigData.AcceptAnyPort ||\r | |
84b5c78e | 1705 | EFI_IP4_EQUAL (&Instance->ConfigData.StationAddress, &mZeroIp4Addr)) {\r |
8a67d61d | 1706 | //\r |
1707 | // Don't try to deliver the ICMP error to this instance if it is not configured,\r | |
1708 | // or it's configured to be promiscuous or accept any port or accept all the\r | |
1709 | // datagrams.\r | |
1710 | //\r | |
1711 | continue;\r | |
1712 | }\r | |
1713 | \r | |
1714 | if (Udp4MatchDgram (Instance, &Udp4Session)) {\r | |
1715 | //\r | |
1716 | // Translate the Icmp Error code according to the udp spec.\r | |
1717 | //\r | |
1718 | Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, NULL, NULL);\r | |
1719 | \r | |
1720 | if (IcmpError > ICMP_ERR_UNREACH_PORT) {\r | |
1721 | Instance->IcmpError = EFI_ICMP_ERROR;\r | |
1722 | }\r | |
1723 | \r | |
1724 | //\r | |
1725 | // Notify the instance with the received Icmp Error.\r | |
1726 | //\r | |
1727 | Udp4ReportIcmpError (Instance);\r | |
1728 | \r | |
1729 | break;\r | |
1730 | }\r | |
1731 | }\r | |
1732 | \r | |
1733 | NetbufFree (Packet);\r | |
1734 | }\r | |
1735 | \r | |
1736 | \r | |
1737 | /**\r | |
1738 | This function reports the received ICMP error.\r | |
1739 | \r | |
1740 | @param Instance Pointer to the udp instance context data.\r | |
1741 | \r | |
1742 | @return None.\r | |
1743 | \r | |
1744 | **/\r | |
1745 | VOID\r | |
1746 | Udp4ReportIcmpError (\r | |
1747 | IN UDP4_INSTANCE_DATA *Instance\r | |
1748 | )\r | |
1749 | {\r | |
1750 | EFI_UDP4_COMPLETION_TOKEN *Token;\r | |
1751 | \r | |
1752 | if (NetMapIsEmpty (&Instance->RxTokens)) {\r | |
1753 | //\r | |
1754 | // There are no receive tokens to deliver the ICMP error.\r | |
1755 | //\r | |
1756 | return;\r | |
1757 | }\r | |
1758 | \r | |
1759 | if (EFI_ERROR (Instance->IcmpError)) {\r | |
1760 | //\r | |
1761 | // Try to get a RxToken from the RxTokens map.\r | |
1762 | //\r | |
1763 | Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);\r | |
1764 | \r | |
1765 | if (Token != NULL) {\r | |
1766 | //\r | |
1767 | // Report the error through the Token.\r | |
1768 | //\r | |
1769 | Token->Status = Instance->IcmpError;\r | |
1770 | gBS->SignalEvent (Token->Event);\r | |
1771 | \r | |
1772 | //\r | |
1773 | // Clear the IcmpError.\r | |
1774 | //\r | |
1775 | Instance->IcmpError = EFI_SUCCESS;\r | |
1776 | }\r | |
1777 | }\r | |
1778 | }\r | |
1779 | \r | |
1780 | \r | |
1781 | /**\r | |
1782 | This function is a dummy ext-free function for the NET_BUF created for the output\r | |
1783 | udp datagram.\r | |
1784 | \r | |
1785 | @param Context Pointer to the context data.\r | |
1786 | \r | |
1787 | @return None.\r | |
1788 | \r | |
1789 | **/\r | |
1790 | VOID\r | |
1791 | Udp4NetVectorExtFree (\r | |
1792 | VOID *Context\r | |
1793 | )\r | |
1794 | {\r | |
1795 | }\r | |
1796 | \r | |
1797 | \r | |
1798 | /**\r | |
1799 | Set the Udp4 variable data.\r | |
1800 | \r | |
1801 | @param Udp4Service Udp4 service data.\r | |
1802 | \r | |
1803 | @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the\r | |
1804 | variable.\r | |
1805 | @retval other Set variable failed.\r | |
1806 | \r | |
1807 | **/\r | |
1808 | EFI_STATUS\r | |
1809 | Udp4SetVariableData (\r | |
1810 | IN UDP4_SERVICE_DATA *Udp4Service\r | |
1811 | )\r | |
1812 | {\r | |
1813 | UINT32 NumConfiguredInstance;\r | |
1814 | NET_LIST_ENTRY *Entry;\r | |
1815 | UINTN VariableDataSize;\r | |
1816 | EFI_UDP4_VARIABLE_DATA *Udp4VariableData;\r | |
1817 | EFI_UDP4_SERVICE_POINT *Udp4ServicePoint;\r | |
1818 | UDP4_INSTANCE_DATA *Udp4Instance;\r | |
1819 | CHAR16 *NewMacString;\r | |
1820 | EFI_STATUS Status;\r | |
1821 | \r | |
1822 | NumConfiguredInstance = 0;\r | |
1823 | \r | |
1824 | //\r | |
1825 | // Go through the children list to count the configured children.\r | |
1826 | //\r | |
1827 | NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r | |
1828 | Udp4Instance = NET_LIST_USER_STRUCT_S (\r | |
1829 | Entry,\r | |
1830 | UDP4_INSTANCE_DATA,\r | |
1831 | Link,\r | |
1832 | UDP4_INSTANCE_DATA_SIGNATURE\r | |
1833 | );\r | |
1834 | \r | |
1835 | if (Udp4Instance->Configured) {\r | |
1836 | NumConfiguredInstance++;\r | |
1837 | }\r | |
1838 | }\r | |
1839 | \r | |
1840 | //\r | |
1841 | // Calculate the size of the Udp4VariableData. As there may be no Udp4 child,\r | |
1842 | // we should add extra buffer for the service points only if the number of configured\r | |
1843 | // children is more than 1.\r | |
1844 | //\r | |
1845 | VariableDataSize = sizeof (EFI_UDP4_VARIABLE_DATA);\r | |
1846 | \r | |
1847 | if (NumConfiguredInstance > 1) {\r | |
1848 | VariableDataSize += sizeof (EFI_UDP4_SERVICE_POINT) * (NumConfiguredInstance - 1);\r | |
1849 | }\r | |
1850 | \r | |
1851 | Udp4VariableData = NetAllocatePool (VariableDataSize);\r | |
1852 | if (Udp4VariableData == NULL) {\r | |
1853 | return EFI_OUT_OF_RESOURCES;\r | |
1854 | }\r | |
1855 | \r | |
1856 | Udp4VariableData->DriverHandle = Udp4Service->ImageHandle;\r | |
1857 | Udp4VariableData->ServiceCount = NumConfiguredInstance;\r | |
1858 | \r | |
1859 | Udp4ServicePoint = &Udp4VariableData->Services[0];\r | |
1860 | \r | |
1861 | //\r | |
1862 | // Go through the children list to fill the configured children's address pairs.\r | |
1863 | //\r | |
1864 | NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r | |
1865 | Udp4Instance = NET_LIST_USER_STRUCT_S (\r | |
1866 | Entry,\r | |
1867 | UDP4_INSTANCE_DATA,\r | |
1868 | Link,\r | |
1869 | UDP4_INSTANCE_DATA_SIGNATURE\r | |
1870 | );\r | |
1871 | \r | |
1872 | if (Udp4Instance->Configured) {\r | |
1873 | Udp4ServicePoint->InstanceHandle = Udp4Instance->ChildHandle;\r | |
1874 | Udp4ServicePoint->LocalAddress = Udp4Instance->ConfigData.StationAddress;\r | |
1875 | Udp4ServicePoint->LocalPort = Udp4Instance->ConfigData.StationPort;\r | |
1876 | Udp4ServicePoint->RemoteAddress = Udp4Instance->ConfigData.RemoteAddress;\r | |
1877 | Udp4ServicePoint->RemotePort = Udp4Instance->ConfigData.RemotePort;\r | |
1878 | \r | |
1879 | Udp4ServicePoint++;\r | |
1880 | }\r | |
1881 | }\r | |
1882 | \r | |
1883 | //\r | |
1884 | // Get the mac string.\r | |
1885 | //\r | |
1886 | Status = NetLibGetMacString (\r | |
1887 | Udp4Service->ControllerHandle,\r | |
1888 | Udp4Service->ImageHandle,\r | |
1889 | &NewMacString\r | |
1890 | );\r | |
1891 | if (EFI_ERROR (Status)) {\r | |
1892 | goto ON_ERROR;\r | |
1893 | }\r | |
1894 | \r | |
1895 | if (Udp4Service->MacString != NULL) {\r | |
1896 | //\r | |
1897 | // The variable is set already, we're going to update it.\r | |
1898 | //\r | |
1899 | if (StrCmp (Udp4Service->MacString, NewMacString) != 0) {\r | |
1900 | //\r | |
1901 | // The mac address is changed, delete the previous variable first.\r | |
1902 | //\r | |
1903 | gRT->SetVariable (\r | |
1904 | Udp4Service->MacString,\r | |
1905 | &gEfiUdp4ServiceBindingProtocolGuid,\r | |
1906 | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r | |
1907 | 0,\r | |
1908 | NULL\r | |
1909 | );\r | |
1910 | }\r | |
1911 | \r | |
1912 | NetFreePool (Udp4Service->MacString);\r | |
1913 | }\r | |
1914 | \r | |
1915 | Udp4Service->MacString = NewMacString;\r | |
1916 | \r | |
1917 | Status = gRT->SetVariable (\r | |
1918 | Udp4Service->MacString,\r | |
1919 | &gEfiUdp4ServiceBindingProtocolGuid,\r | |
1920 | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r | |
1921 | VariableDataSize,\r | |
1922 | (VOID *) Udp4VariableData\r | |
1923 | );\r | |
1924 | \r | |
1925 | ON_ERROR:\r | |
1926 | \r | |
1927 | NetFreePool (Udp4VariableData);\r | |
1928 | \r | |
1929 | return Status;\r | |
1930 | }\r | |
1931 | \r | |
1932 | \r | |
1933 | /**\r | |
1934 | Clear the variable and free the resource.\r | |
1935 | \r | |
1936 | @param Udp4Service Udp4 service data.\r | |
1937 | \r | |
1938 | @return None.\r | |
1939 | \r | |
1940 | **/\r | |
1941 | VOID\r | |
1942 | Udp4ClearVariableData (\r | |
1943 | IN UDP4_SERVICE_DATA *Udp4Service\r | |
1944 | )\r | |
1945 | {\r | |
1946 | ASSERT (Udp4Service->MacString != NULL);\r | |
1947 | \r | |
1948 | gRT->SetVariable (\r | |
1949 | Udp4Service->MacString,\r | |
1950 | &gEfiUdp4ServiceBindingProtocolGuid,\r | |
1951 | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r | |
1952 | 0,\r | |
1953 | NULL\r | |
1954 | );\r | |
1955 | \r | |
1956 | NetFreePool (Udp4Service->MacString);\r | |
1957 | Udp4Service->MacString = NULL;\r | |
1958 | }\r |