]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Output.c
Update network drivers to use FreePool() instead of gBS->FreePool().
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Output.c
... / ...
CommitLineData
1/** @file\r
2 Transmit the IP4 packet.\r
3 \r
4Copyright (c) 2005 - 2009, Intel Corporation.<BR>\r
5All rights reserved. This program and the accompanying materials\r
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
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
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
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
40 IN OUT NET_BUF *Packet,\r
41 IN IP4_HEAD *Head,\r
42 IN UINT8 *Option,\r
43 IN UINT32 OptLen\r
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
60 ASSERT (((Len % 4) == 0) && (HeadLen <= IP4_MAX_HEADLEN));\r
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
76 PacketHead->TotalLen = HTONS (Packet->TotalSize);\r
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
84 PacketHead->Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) PacketHead, HeadLen));\r
85\r
86 Packet->Ip.Ip4 = PacketHead;\r
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
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
99\r
100 @return NULL if no proper interface is found, otherwise the interface that\r
101 can be used to send the system packet from.\r
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
113 LIST_ENTRY *Entry;\r
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
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
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
187 NULL if the packet is from the system.\r
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
190 the following header fields: Tos, TotalLen, Id, tl,\r
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
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
199 255.255.255.255 means broadcast.\r
200 @param[in] Callback The callback function to issue when transmission\r
201 completed.\r
202 @param[in] Context The opaque context for the callback\r
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
207 @retval Others Failed to transmit the packet.\r
208\r
209**/\r
210EFI_STATUS\r
211Ip4Output (\r
212 IN IP4_SERVICE *IpSb,\r
213 IN IP4_PROTOCOL *IpInstance OPTIONAL,\r
214 IN NET_BUF *Packet,\r
215 IN IP4_HEAD *Head,\r
216 IN UINT8 *Option,\r
217 IN UINT32 OptLen,\r
218 IN IP4_ADDR GateWay,\r
219 IN IP4_FRAME_CALLBACK Callback,\r
220 IN VOID *Context\r
221 )\r
222{\r
223 IP4_INTERFACE *IpIf;\r
224 IP4_ROUTE_CACHE_ENTRY *CacheEntry;\r
225 IP4_ADDR Dest;\r
226 EFI_STATUS Status;\r
227 NET_BUF *Fragment;\r
228 UINT32 Index;\r
229 UINT32 HeadLen;\r
230 UINT32 PacketLen;\r
231 UINT32 Offset;\r
232 UINT32 Mtu;\r
233 UINT32 Num;\r
234\r
235 //\r
236 // Select an interface/source for system packet, application\r
237 // should select them itself.\r
238 //\r
239 if (IpInstance == NULL) {\r
240 IpIf = Ip4SelectInterface (IpSb, Head->Dst, Head->Src);\r
241 } else {\r
242 IpIf = IpInstance->Interface;\r
243 }\r
244\r
245 if (IpIf == NULL) {\r
246 return EFI_NO_MAPPING;\r
247 }\r
248\r
249 if ((Head->Src == IP4_ALLZERO_ADDRESS) && (IpInstance == NULL)) {\r
250 Head->Src = IpIf->Ip;\r
251 }\r
252\r
253 //\r
254 // Route the packet unless overrided, that is, GateWay isn't zero.\r
255 //\r
256 if (GateWay == IP4_ALLZERO_ADDRESS) {\r
257 Dest = Head->Dst;\r
258\r
259 if (IP4_IS_BROADCAST (Ip4GetNetCast (Dest, IpIf)) || (Dest == IP4_ALLONE_ADDRESS)) {\r
260 //\r
261 // Set the gateway to local broadcast if the Dest is\r
262 // the broadcast address for the connected network or\r
263 // it is local broadcast.\r
264 //\r
265 GateWay = IP4_ALLONE_ADDRESS;\r
266\r
267 } else if (IP4_IS_MULTICAST (Dest)) {\r
268 //\r
269 // Set the gateway to the destination if it is an multicast\r
270 // address. The IP4_INTERFACE won't consult ARP to send local\r
271 // broadcast and multicast.\r
272 //\r
273 GateWay = Head->Dst;\r
274\r
275 } else {\r
276 //\r
277 // Consult the route table to route the packet\r
278 //\r
279 if (IpInstance == NULL) {\r
280 CacheEntry = Ip4Route (IpSb->DefaultRouteTable, Head->Dst, Head->Src);\r
281 } else {\r
282 CacheEntry = Ip4Route (IpInstance->RouteTable, Head->Dst, Head->Src);\r
283 }\r
284\r
285 if (CacheEntry == NULL) {\r
286 return EFI_NOT_FOUND;\r
287 }\r
288\r
289 GateWay = CacheEntry->NextHop;\r
290 Ip4FreeRouteCacheEntry (CacheEntry);\r
291 }\r
292 }\r
293\r
294 //\r
295 // TODO: currently Option/OptLen are not included into encryption scope.\r
296 //\r
297 Status = Ip4IpSecProcessPacket (\r
298 IpSb, \r
299 Head, \r
300 &Packet, \r
301 Option, \r
302 OptLen, \r
303 EfiIPsecOutBound,\r
304 Context\r
305 );\r
306\r
307 if (EFI_ERROR(Status)) {\r
308 return Status;\r
309 }\r
310\r
311 //\r
312 // OK, selected the source and route, fragment the packet then send\r
313 // them. Tag each fragment other than the first one as spawn from it.\r
314\r
315 //\r
316 // IPsec payload has been appended, so use IpSb->SnpMode.MaxPacketSize here.\r
317 //\r
318 Mtu = IpSb->SnpMode.MaxPacketSize;\r
319 HeadLen = sizeof (IP4_HEAD) + ((OptLen + 3) & (~0x03));\r
320 Head->Id = mIp4Id++;\r
321\r
322 if (Packet->TotalSize + HeadLen > Mtu) {\r
323 //\r
324 // Packet is fragmented from the tail to the head, that is, the\r
325 // first frame sent is the last fragment of the packet. The first\r
326 // fragment is NOT sent in this loop. First compute how many\r
327 // fragments there are.\r
328 //\r
329 Mtu = (Mtu - HeadLen) & (~0x07);\r
330 Num = (Packet->TotalSize + Mtu - 1) / Mtu;\r
331\r
332 //\r
333 // Initialize the packet length and Offset. Other than the last\r
334 // fragment, the packet length equals to MTU. The offset is always\r
335 // aligned to MTU.\r
336 //\r
337 PacketLen = Packet->TotalSize - (Num - 1) * Mtu;\r
338 Offset = Mtu * (Num - 1);\r
339\r
340 for (Index = 0; Index < Num - 1; Index++, Offset -= Mtu) {\r
341 Fragment = NetbufGetFragment (Packet, Offset, PacketLen, IP4_MAX_HEADLEN);\r
342\r
343 if (Fragment == NULL) {\r
344 Status = EFI_OUT_OF_RESOURCES;\r
345 goto ON_ERROR;\r
346 }\r
347\r
348 //\r
349 // Update the header's fragment. The caller fills the IP4 header\r
350 // fields that are required by Ip4PrependHead except the fragment.\r
351 //\r
352 Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, (Index != 0), Offset);\r
353 Ip4PrependHead (Fragment, Head, Option, OptLen);\r
354\r
355 //\r
356 // Transmit the fragments, pass the Packet address as the context.\r
357 // So, we can find all the fragments spawned from the Packet by\r
358 // compare the NetBuf and Context to the Packet.\r
359 //\r
360 Status = Ip4SendFrame (\r
361 IpIf,\r
362 IpInstance,\r
363 Fragment,\r
364 GateWay,\r
365 Ip4SysPacketSent,\r
366 Packet\r
367 );\r
368\r
369 if (EFI_ERROR (Status)) {\r
370 goto ON_ERROR;\r
371 }\r
372\r
373 PacketLen = Mtu;\r
374 }\r
375\r
376 //\r
377 // Trim the already sent data, then adjust the head's fragment field.\r
378 //\r
379 NetbufTrim (Packet, Packet->TotalSize - Mtu, FALSE);\r
380 Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, TRUE, 0);\r
381 }\r
382\r
383 //\r
384 // Send the first fragment, it is either the orginal packet, or the\r
385 // first fragment of a fragmented packet. It seems that there is a subtle\r
386 // bug here: what if the caller free the packet in Callback and IpIf (or\r
387 // MNP child used by that interface) still holds the fragments and try\r
388 // to access the data? The caller can free the packet if it recycles the\r
389 // consumer's (such as UDP) data in the Callback. But this can't happen.\r
390 // The detailed sequence is:\r
391 // 1. for the packets generated by IP4 driver itself:\r
392 // The Callback is Ip4SysPacketSent, which is the same as the\r
393 // fragments' callback. Ip4SysPacketSent simply calls NetbufFree\r
394 // to release its reference to the packet. So, no problem for\r
395 // system packets.\r
396 //\r
397 // 2. for the upper layer's packets (use UDP as an example):\r
398 // UDP requests the IP layer to transmit some data which is\r
399 // wrapped in an asynchronous token, the token is wrapped\r
400 // in IP4_TXTOKEN_WRAP by IP4. IP4 also wrap the user's data\r
401 // in a net buffer, which is Packet we get here. IP4_TXTOKEN_WRAP\r
402 // is bound with the Packet. It will only be freed when all\r
403 // the references to Packet have been released. Upon then, the\r
404 // Packet's OnFree callback will release the IP4_TXTOKEN_WRAP,\r
405 // and singal the user's recycle event. So, also no problem for\r
406 // upper layer's packets.\r
407 //\r
408 Ip4PrependHead (Packet, Head, Option, OptLen);\r
409 Status = Ip4SendFrame (IpIf, IpInstance, Packet, GateWay, Callback, Context);\r
410\r
411 if (EFI_ERROR (Status)) {\r
412 goto ON_ERROR;\r
413 }\r
414\r
415 return EFI_SUCCESS;\r
416\r
417ON_ERROR:\r
418 Ip4CancelPacket (IpIf, Packet, Status);\r
419 return Status;\r
420}\r
421\r
422\r
423/**\r
424 The filter function to find a packet and all its fragments.\r
425 The packet's fragments have their Context set to the packet.\r
426\r
427 @param[in] Frame The frames hold by the low level interface\r
428 @param[in] Context Context to the function, which is the packet.\r
429\r
430 @retval TRUE This is the packet to cancel or its fragments.\r
431 @retval FALSE This is unrelated packet.\r
432\r
433**/\r
434BOOLEAN\r
435Ip4CancelPacketFragments (\r
436 IN IP4_LINK_TX_TOKEN *Frame,\r
437 IN VOID *Context\r
438 )\r
439{\r
440 if ((Frame->Packet == (NET_BUF *) Context) || (Frame->Context == Context)) {\r
441 return TRUE;\r
442 }\r
443\r
444 return FALSE;\r
445}\r
446\r
447\r
448/**\r
449 Cancel the Packet and all its fragments.\r
450\r
451 @param IpIf The interface from which the Packet is sent\r
452 @param Packet The Packet to cancel\r
453 @param IoStatus The status returns to the sender.\r
454\r
455**/\r
456VOID\r
457Ip4CancelPacket (\r
458 IN IP4_INTERFACE *IpIf,\r
459 IN NET_BUF *Packet,\r
460 IN EFI_STATUS IoStatus\r
461 )\r
462{\r
463 Ip4CancelFrames (IpIf, IoStatus, Ip4CancelPacketFragments, Packet);\r
464}\r