]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Output.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Output.c
CommitLineData
772db4bb 1/** @file\r
3e8c18da 2 Transmit the IP4 packet.\r
d1102dba
LG
3\r
4Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 5This program and the accompanying materials\r
772db4bb 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
772db4bb 13**/\r
14\r
15#include "Ip4Impl.h"\r
16\r
17UINT16 mIp4Id;\r
18\r
19\r
20/**\r
21 Prepend an IP4 head to the Packet. It will copy the options and\r
22 build the IP4 header fields. Used for IP4 fragmentation.\r
23\r
3e8c18da 24 @param Packet The packet to prepend IP4 header to\r
25 @param Head The caller supplied header. The caller should set\r
26 the following header fields: Tos, TotalLen, Id,\r
27 Fragment, Ttl, Protocol, Src and Dst. All the fields\r
28 are in host byte order. This function will fill in\r
29 the Ver, HeadLen, and checksum.\r
30 @param Option The orginal IP4 option to copy from\r
31 @param OptLen The length of the IP4 option\r
772db4bb 32\r
33 @retval EFI_BAD_BUFFER_SIZE There is no enought room in the head space of\r
34 Packet.\r
35 @retval EFI_SUCCESS The IP4 header is successfully added to the packet.\r
36\r
37**/\r
38EFI_STATUS\r
39Ip4PrependHead (\r
96e1079f 40 IN OUT NET_BUF *Packet,\r
41 IN IP4_HEAD *Head,\r
42 IN UINT8 *Option,\r
43 IN UINT32 OptLen\r
772db4bb 44 )\r
45{\r
46 UINT32 HeadLen;\r
47 UINT32 Len;\r
48 IP4_HEAD *PacketHead;\r
49 BOOLEAN FirstFragment;\r
50\r
51 //\r
52 // Prepend the options: first get the option length, then copy it over.\r
53 //\r
54 HeadLen = 0;\r
55 FirstFragment = IP4_FIRST_FRAGMENT (Head->Fragment);\r
56\r
57 Ip4CopyOption (Option, OptLen, FirstFragment, NULL, &Len);\r
58\r
59 HeadLen = IP4_MIN_HEADLEN + Len;\r
96e1079f 60 ASSERT (((Len % 4) == 0) && (HeadLen <= IP4_MAX_HEADLEN));\r
772db4bb 61\r
62 PacketHead = (IP4_HEAD *) NetbufAllocSpace (Packet, HeadLen, NET_BUF_HEAD);\r
63\r
64 if (PacketHead == NULL) {\r
65 return EFI_BAD_BUFFER_SIZE;\r
66 }\r
67\r
68 Ip4CopyOption (Option, OptLen, FirstFragment, (UINT8 *) (PacketHead + 1), &Len);\r
69\r
70 //\r
71 // Set the head up, convert the host byte order to network byte order\r
72 //\r
73 PacketHead->Ver = 4;\r
74 PacketHead->HeadLen = (UINT8) (HeadLen >> 2);\r
75 PacketHead->Tos = Head->Tos;\r
1204fe83 76 PacketHead->TotalLen = HTONS ((UINT16) Packet->TotalSize);\r
772db4bb 77 PacketHead->Id = HTONS (Head->Id);\r
78 PacketHead->Fragment = HTONS (Head->Fragment);\r
79 PacketHead->Checksum = 0;\r
80 PacketHead->Ttl = Head->Ttl;\r
81 PacketHead->Protocol = Head->Protocol;\r
82 PacketHead->Src = HTONL (Head->Src);\r
83 PacketHead->Dst = HTONL (Head->Dst);\r
687a2e5f 84 PacketHead->Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) PacketHead, HeadLen));\r
772db4bb 85\r
f6b7393c 86 Packet->Ip.Ip4 = PacketHead;\r
772db4bb 87 return EFI_SUCCESS;\r
88}\r
89\r
90\r
91/**\r
92 Select an interface to send the packet generated in the IP4 driver\r
93 itself, that is, not by the requests of IP4 child's consumer. Such\r
94 packets include the ICMP echo replies, and other ICMP error packets.\r
95\r
3e8c18da 96 @param[in] IpSb The IP4 service that wants to send the packets.\r
97 @param[in] Dst The destination of the packet\r
98 @param[in] Src The source of the packet\r
772db4bb 99\r
100 @return NULL if no proper interface is found, otherwise the interface that\r
96e1079f 101 can be used to send the system packet from.\r
772db4bb 102\r
103**/\r
104IP4_INTERFACE *\r
105Ip4SelectInterface (\r
106 IN IP4_SERVICE *IpSb,\r
107 IN IP4_ADDR Dst,\r
108 IN IP4_ADDR Src\r
109 )\r
110{\r
111 IP4_INTERFACE *IpIf;\r
112 IP4_INTERFACE *Selected;\r
e48e37fc 113 LIST_ENTRY *Entry;\r
772db4bb 114\r
115 //\r
116 // Select the interface the Dst is on if one of the connected\r
117 // network. Some IP instance may be configured with 0.0.0.0/0,\r
118 // don't select that interface now.\r
119 //\r
120 IpIf = Ip4FindNet (IpSb, Dst);\r
121\r
122 if ((IpIf != NULL) && (IpIf->Ip != IP4_ALLZERO_ADDRESS)) {\r
123 return IpIf;\r
124 }\r
125\r
126 //\r
127 // If source is one of the interface address, select it.\r
128 //\r
129 IpIf = Ip4FindInterface (IpSb, Src);\r
130\r
131 if ((IpIf != NULL) && (IpIf->Ip != IP4_ALLZERO_ADDRESS)) {\r
132 return IpIf;\r
133 }\r
134\r
135 //\r
136 // Select a configured interface as the fall back. Always prefer\r
137 // an interface with non-zero address.\r
138 //\r
139 Selected = NULL;\r
140\r
141 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
142 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
143\r
144 if (IpIf->Configured && ((Selected == NULL) || (Selected->Ip == 0))) {\r
145 Selected = IpIf;\r
146 }\r
147 }\r
148\r
149 return Selected;\r
150}\r
151\r
152\r
153/**\r
154 The default callback function for system generated packet.\r
155 It will free the packet.\r
156\r
157 @param Ip4Instance The IP4 child that issued the transmission. It most\r
158 like is NULL.\r
159 @param Packet The packet that transmitted.\r
160 @param IoStatus The result of the transmission, succeeded or failed.\r
161 @param LinkFlag Not used when transmission. check IP4_FRAME_CALLBACK\r
162 for reference.\r
163 @param Context The context provided by us\r
164\r
772db4bb 165**/\r
166VOID\r
167Ip4SysPacketSent (\r
168 IP4_PROTOCOL *Ip4Instance,\r
169 NET_BUF *Packet,\r
170 EFI_STATUS IoStatus,\r
171 UINT32 LinkFlag,\r
172 VOID *Context\r
173 )\r
174{\r
175 NetbufFree (Packet);\r
176}\r
177\r
178\r
179/**\r
180 Transmit an IP4 packet. The packet comes either from the IP4\r
181 child's consumer (IpInstance != NULL) or the IP4 driver itself\r
182 (IpInstance == NULL). It will route the packet, fragment it,\r
183 then transmit all the fragments through some interface.\r
184\r
3e8c18da 185 @param[in] IpSb The IP4 service instance to transmit the packet\r
186 @param[in] IpInstance The IP4 child that issues the transmission. It is\r
772db4bb 187 NULL if the packet is from the system.\r
3e8c18da 188 @param[in] Packet The user data to send, excluding the IP header.\r
189 @param[in] Head The caller supplied header. The caller should set\r
96e1079f 190 the following header fields: Tos, TotalLen, Id, tl,\r
772db4bb 191 Fragment, Protocol, Src and Dst. All the fields are\r
192 in host byte order. This function will fill in the\r
193 Ver, HeadLen, Fragment, and checksum. The Fragment\r
194 only need to include the DF flag. Ip4Output will\r
195 compute the MF and offset for you.\r
3e8c18da 196 @param[in] Option The original option to append to the IP headers\r
197 @param[in] OptLen The length of the option\r
198 @param[in] GateWay The next hop address to transmit packet to.\r
772db4bb 199 255.255.255.255 means broadcast.\r
3e8c18da 200 @param[in] Callback The callback function to issue when transmission\r
772db4bb 201 completed.\r
3e8c18da 202 @param[in] Context The opaque context for the callback\r
772db4bb 203\r
204 @retval EFI_NO_MAPPING There is no interface to the destination.\r
205 @retval EFI_NOT_FOUND There is no route to the destination\r
206 @retval EFI_SUCCESS The packet is successfully transmitted.\r
216f7970 207 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length +\r
208 total data length is greater than MTU (or greater\r
209 than the maximum packet size if Token.Packet.TxData.\r
210 OverrideData.DoNotFragment is TRUE.)\r
772db4bb 211 @retval Others Failed to transmit the packet.\r
212\r
213**/\r
214EFI_STATUS\r
215Ip4Output (\r
216 IN IP4_SERVICE *IpSb,\r
24af132f 217 IN IP4_PROTOCOL *IpInstance OPTIONAL,\r
772db4bb 218 IN NET_BUF *Packet,\r
219 IN IP4_HEAD *Head,\r
220 IN UINT8 *Option,\r
221 IN UINT32 OptLen,\r
222 IN IP4_ADDR GateWay,\r
223 IN IP4_FRAME_CALLBACK Callback,\r
224 IN VOID *Context\r
225 )\r
226{\r
227 IP4_INTERFACE *IpIf;\r
228 IP4_ROUTE_CACHE_ENTRY *CacheEntry;\r
229 IP4_ADDR Dest;\r
230 EFI_STATUS Status;\r
231 NET_BUF *Fragment;\r
232 UINT32 Index;\r
233 UINT32 HeadLen;\r
234 UINT32 PacketLen;\r
235 UINT32 Offset;\r
236 UINT32 Mtu;\r
237 UINT32 Num;\r
216f7970 238 BOOLEAN RawData;\r
772db4bb 239\r
240 //\r
241 // Select an interface/source for system packet, application\r
242 // should select them itself.\r
243 //\r
244 if (IpInstance == NULL) {\r
245 IpIf = Ip4SelectInterface (IpSb, Head->Dst, Head->Src);\r
246 } else {\r
247 IpIf = IpInstance->Interface;\r
248 }\r
249\r
250 if (IpIf == NULL) {\r
251 return EFI_NO_MAPPING;\r
252 }\r
253\r
254 if ((Head->Src == IP4_ALLZERO_ADDRESS) && (IpInstance == NULL)) {\r
255 Head->Src = IpIf->Ip;\r
256 }\r
257\r
705f53a9 258 //\r
259 // Before IPsec process, prepared the IP head.\r
216f7970 260 // If Ip4Output is transmitting RawData, don't update IPv4 header.\r
705f53a9 261 //\r
216f7970 262 HeadLen = sizeof (IP4_HEAD) + ((OptLen + 3) & (~0x03));\r
263\r
264 if ((IpInstance != NULL) && IpInstance->ConfigData.RawData) {\r
265 RawData = TRUE;\r
266 } else {\r
267 Head->HeadLen = (UINT8) (HeadLen >> 2);\r
268 Head->Id = mIp4Id++;\r
269 Head->Ver = 4;\r
270 RawData = FALSE;\r
271 }\r
d1102dba 272\r
705f53a9 273 //\r
274 // Call IPsec process.\r
275 //\r
276 Status = Ip4IpSecProcessPacket (\r
d1102dba
LG
277 IpSb,\r
278 &Head,\r
279 &Packet,\r
280 &Option,\r
281 &OptLen,\r
705f53a9 282 EfiIPsecOutBound,\r
283 Context\r
284 );\r
285\r
286 if (EFI_ERROR(Status)) {\r
287 return Status;\r
288 }\r
d1102dba 289\r
44a97ac6 290 Dest = Head->Dst;\r
291 if (IP4_IS_BROADCAST (Ip4GetNetCast (Dest, IpIf)) || (Dest == IP4_ALLONE_ADDRESS)) {\r
292 //\r
293 // Set the gateway to local broadcast if the Dest is\r
294 // the broadcast address for the connected network or\r
295 // it is local broadcast.\r
296 //\r
297 GateWay = IP4_ALLONE_ADDRESS;\r
298\r
299 } else if (IP4_IS_MULTICAST (Dest)) {\r
300 //\r
301 // Set the gateway to the destination if it is an multicast\r
302 // address. The IP4_INTERFACE won't consult ARP to send local\r
303 // broadcast and multicast.\r
304 //\r
305 GateWay = Head->Dst;\r
306\r
307 } else if (GateWay == IP4_ALLZERO_ADDRESS) {\r
308 //\r
309 // Route the packet unless overrided, that is, GateWay isn't zero.\r
310 //\r
311 if (IpInstance == NULL) {\r
312 CacheEntry = Ip4Route (IpSb->DefaultRouteTable, Head->Dst, Head->Src);\r
772db4bb 313 } else {\r
44a97ac6 314 CacheEntry = Ip4Route (IpInstance->RouteTable, Head->Dst, Head->Src);\r
1f6729ff 315 //\r
316 // If failed to route the packet by using the instance's route table,\r
317 // try to use the default route table.\r
318 //\r
319 if (CacheEntry == NULL) {\r
320 CacheEntry = Ip4Route (IpSb->DefaultRouteTable, Head->Dst, Head->Src);\r
321 }\r
44a97ac6 322 }\r
323\r
324 if (CacheEntry == NULL) {\r
325 return EFI_NOT_FOUND;\r
772db4bb 326 }\r
44a97ac6 327\r
328 GateWay = CacheEntry->NextHop;\r
329 Ip4FreeRouteCacheEntry (CacheEntry);\r
772db4bb 330 }\r
331\r
332 //\r
333 // OK, selected the source and route, fragment the packet then send\r
334 // them. Tag each fragment other than the first one as spawn from it.\r
335 //\r
d1102dba 336 Mtu = IpSb->MaxPacketSize + sizeof (IP4_HEAD);\r
772db4bb 337\r
338 if (Packet->TotalSize + HeadLen > Mtu) {\r
216f7970 339 //\r
340 // Fragmentation is diabled for RawData mode.\r
341 //\r
342 if (RawData) {\r
343 return EFI_BAD_BUFFER_SIZE;\r
344 }\r
d1102dba 345\r
772db4bb 346 //\r
347 // Packet is fragmented from the tail to the head, that is, the\r
348 // first frame sent is the last fragment of the packet. The first\r
349 // fragment is NOT sent in this loop. First compute how many\r
350 // fragments there are.\r
351 //\r
352 Mtu = (Mtu - HeadLen) & (~0x07);\r
353 Num = (Packet->TotalSize + Mtu - 1) / Mtu;\r
354\r
355 //\r
356 // Initialize the packet length and Offset. Other than the last\r
357 // fragment, the packet length equals to MTU. The offset is always\r
358 // aligned to MTU.\r
359 //\r
360 PacketLen = Packet->TotalSize - (Num - 1) * Mtu;\r
361 Offset = Mtu * (Num - 1);\r
362\r
363 for (Index = 0; Index < Num - 1; Index++, Offset -= Mtu) {\r
364 Fragment = NetbufGetFragment (Packet, Offset, PacketLen, IP4_MAX_HEADLEN);\r
365\r
366 if (Fragment == NULL) {\r
367 Status = EFI_OUT_OF_RESOURCES;\r
368 goto ON_ERROR;\r
369 }\r
370\r
371 //\r
372 // Update the header's fragment. The caller fills the IP4 header\r
373 // fields that are required by Ip4PrependHead except the fragment.\r
374 //\r
375 Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, (Index != 0), Offset);\r
376 Ip4PrependHead (Fragment, Head, Option, OptLen);\r
377\r
378 //\r
379 // Transmit the fragments, pass the Packet address as the context.\r
380 // So, we can find all the fragments spawned from the Packet by\r
381 // compare the NetBuf and Context to the Packet.\r
382 //\r
383 Status = Ip4SendFrame (\r
384 IpIf,\r
385 IpInstance,\r
386 Fragment,\r
387 GateWay,\r
388 Ip4SysPacketSent,\r
389 Packet\r
390 );\r
391\r
392 if (EFI_ERROR (Status)) {\r
393 goto ON_ERROR;\r
394 }\r
395\r
396 PacketLen = Mtu;\r
397 }\r
398\r
399 //\r
400 // Trim the already sent data, then adjust the head's fragment field.\r
401 //\r
402 NetbufTrim (Packet, Packet->TotalSize - Mtu, FALSE);\r
403 Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, TRUE, 0);\r
404 }\r
405\r
406 //\r
407 // Send the first fragment, it is either the orginal packet, or the\r
408 // first fragment of a fragmented packet. It seems that there is a subtle\r
409 // bug here: what if the caller free the packet in Callback and IpIf (or\r
410 // MNP child used by that interface) still holds the fragments and try\r
411 // to access the data? The caller can free the packet if it recycles the\r
412 // consumer's (such as UDP) data in the Callback. But this can't happen.\r
413 // The detailed sequence is:\r
414 // 1. for the packets generated by IP4 driver itself:\r
415 // The Callback is Ip4SysPacketSent, which is the same as the\r
416 // fragments' callback. Ip4SysPacketSent simply calls NetbufFree\r
417 // to release its reference to the packet. So, no problem for\r
418 // system packets.\r
419 //\r
420 // 2. for the upper layer's packets (use UDP as an example):\r
421 // UDP requests the IP layer to transmit some data which is\r
422 // wrapped in an asynchronous token, the token is wrapped\r
423 // in IP4_TXTOKEN_WRAP by IP4. IP4 also wrap the user's data\r
424 // in a net buffer, which is Packet we get here. IP4_TXTOKEN_WRAP\r
425 // is bound with the Packet. It will only be freed when all\r
426 // the references to Packet have been released. Upon then, the\r
427 // Packet's OnFree callback will release the IP4_TXTOKEN_WRAP,\r
428 // and singal the user's recycle event. So, also no problem for\r
429 // upper layer's packets.\r
430 //\r
431 Ip4PrependHead (Packet, Head, Option, OptLen);\r
432 Status = Ip4SendFrame (IpIf, IpInstance, Packet, GateWay, Callback, Context);\r
433\r
434 if (EFI_ERROR (Status)) {\r
435 goto ON_ERROR;\r
436 }\r
437\r
438 return EFI_SUCCESS;\r
439\r
440ON_ERROR:\r
441 Ip4CancelPacket (IpIf, Packet, Status);\r
442 return Status;\r
443}\r
444\r
445\r
446/**\r
447 The filter function to find a packet and all its fragments.\r
448 The packet's fragments have their Context set to the packet.\r
449\r
3e8c18da 450 @param[in] Frame The frames hold by the low level interface\r
451 @param[in] Context Context to the function, which is the packet.\r
772db4bb 452\r
453 @retval TRUE This is the packet to cancel or its fragments.\r
454 @retval FALSE This is unrelated packet.\r
455\r
456**/\r
772db4bb 457BOOLEAN\r
458Ip4CancelPacketFragments (\r
96e1079f 459 IN IP4_LINK_TX_TOKEN *Frame,\r
460 IN VOID *Context\r
772db4bb 461 )\r
462{\r
463 if ((Frame->Packet == (NET_BUF *) Context) || (Frame->Context == Context)) {\r
464 return TRUE;\r
465 }\r
466\r
467 return FALSE;\r
468}\r
469\r
470\r
471/**\r
472 Cancel the Packet and all its fragments.\r
473\r
474 @param IpIf The interface from which the Packet is sent\r
475 @param Packet The Packet to cancel\r
476 @param IoStatus The status returns to the sender.\r
477\r
772db4bb 478**/\r
479VOID\r
480Ip4CancelPacket (\r
481 IN IP4_INTERFACE *IpIf,\r
482 IN NET_BUF *Packet,\r
483 IN EFI_STATUS IoStatus\r
484 )\r
485{\r
486 Ip4CancelFrames (IpIf, IoStatus, Ip4CancelPacketFragments, Packet);\r
487}\r