]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Output.c
fixed IP fragment issue which caused the MTU used by IP is less than the one IP expos...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Output.c
CommitLineData
772db4bb 1/** @file\r
3e8c18da 2 Transmit the IP4 packet.\r
3 \r
f6b7393c 4Copyright (c) 2005 - 2009, Intel Corporation.<BR>\r
772db4bb 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
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
207 @retval Others Failed to transmit the packet.\r
208\r
209**/\r
210EFI_STATUS\r
211Ip4Output (\r
212 IN IP4_SERVICE *IpSb,\r
24af132f 213 IN IP4_PROTOCOL *IpInstance OPTIONAL,\r
772db4bb 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
96e1079f 262 // the broadcast address for the connected network or\r
263 // it is local broadcast.\r
772db4bb 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
a1503a32 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
772db4bb 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
619561dc 315 Mtu = IpSb->MaxPacketSize + sizeof (IP4_HEAD);\r
84b5c78e 316 HeadLen = sizeof (IP4_HEAD) + ((OptLen + 3) & (~0x03));\r
772db4bb 317 Head->Id = mIp4Id++;\r
318\r
319 if (Packet->TotalSize + HeadLen > Mtu) {\r
320 //\r
321 // Packet is fragmented from the tail to the head, that is, the\r
322 // first frame sent is the last fragment of the packet. The first\r
323 // fragment is NOT sent in this loop. First compute how many\r
324 // fragments there are.\r
325 //\r
326 Mtu = (Mtu - HeadLen) & (~0x07);\r
327 Num = (Packet->TotalSize + Mtu - 1) / Mtu;\r
328\r
329 //\r
330 // Initialize the packet length and Offset. Other than the last\r
331 // fragment, the packet length equals to MTU. The offset is always\r
332 // aligned to MTU.\r
333 //\r
334 PacketLen = Packet->TotalSize - (Num - 1) * Mtu;\r
335 Offset = Mtu * (Num - 1);\r
336\r
337 for (Index = 0; Index < Num - 1; Index++, Offset -= Mtu) {\r
338 Fragment = NetbufGetFragment (Packet, Offset, PacketLen, IP4_MAX_HEADLEN);\r
339\r
340 if (Fragment == NULL) {\r
341 Status = EFI_OUT_OF_RESOURCES;\r
342 goto ON_ERROR;\r
343 }\r
344\r
345 //\r
346 // Update the header's fragment. The caller fills the IP4 header\r
347 // fields that are required by Ip4PrependHead except the fragment.\r
348 //\r
349 Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, (Index != 0), Offset);\r
350 Ip4PrependHead (Fragment, Head, Option, OptLen);\r
351\r
352 //\r
353 // Transmit the fragments, pass the Packet address as the context.\r
354 // So, we can find all the fragments spawned from the Packet by\r
355 // compare the NetBuf and Context to the Packet.\r
356 //\r
357 Status = Ip4SendFrame (\r
358 IpIf,\r
359 IpInstance,\r
360 Fragment,\r
361 GateWay,\r
362 Ip4SysPacketSent,\r
363 Packet\r
364 );\r
365\r
366 if (EFI_ERROR (Status)) {\r
367 goto ON_ERROR;\r
368 }\r
369\r
370 PacketLen = Mtu;\r
371 }\r
372\r
373 //\r
374 // Trim the already sent data, then adjust the head's fragment field.\r
375 //\r
376 NetbufTrim (Packet, Packet->TotalSize - Mtu, FALSE);\r
377 Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, TRUE, 0);\r
378 }\r
379\r
380 //\r
381 // Send the first fragment, it is either the orginal packet, or the\r
382 // first fragment of a fragmented packet. It seems that there is a subtle\r
383 // bug here: what if the caller free the packet in Callback and IpIf (or\r
384 // MNP child used by that interface) still holds the fragments and try\r
385 // to access the data? The caller can free the packet if it recycles the\r
386 // consumer's (such as UDP) data in the Callback. But this can't happen.\r
387 // The detailed sequence is:\r
388 // 1. for the packets generated by IP4 driver itself:\r
389 // The Callback is Ip4SysPacketSent, which is the same as the\r
390 // fragments' callback. Ip4SysPacketSent simply calls NetbufFree\r
391 // to release its reference to the packet. So, no problem for\r
392 // system packets.\r
393 //\r
394 // 2. for the upper layer's packets (use UDP as an example):\r
395 // UDP requests the IP layer to transmit some data which is\r
396 // wrapped in an asynchronous token, the token is wrapped\r
397 // in IP4_TXTOKEN_WRAP by IP4. IP4 also wrap the user's data\r
398 // in a net buffer, which is Packet we get here. IP4_TXTOKEN_WRAP\r
399 // is bound with the Packet. It will only be freed when all\r
400 // the references to Packet have been released. Upon then, the\r
401 // Packet's OnFree callback will release the IP4_TXTOKEN_WRAP,\r
402 // and singal the user's recycle event. So, also no problem for\r
403 // upper layer's packets.\r
404 //\r
405 Ip4PrependHead (Packet, Head, Option, OptLen);\r
406 Status = Ip4SendFrame (IpIf, IpInstance, Packet, GateWay, Callback, Context);\r
407\r
408 if (EFI_ERROR (Status)) {\r
409 goto ON_ERROR;\r
410 }\r
411\r
412 return EFI_SUCCESS;\r
413\r
414ON_ERROR:\r
415 Ip4CancelPacket (IpIf, Packet, Status);\r
416 return Status;\r
417}\r
418\r
419\r
420/**\r
421 The filter function to find a packet and all its fragments.\r
422 The packet's fragments have their Context set to the packet.\r
423\r
3e8c18da 424 @param[in] Frame The frames hold by the low level interface\r
425 @param[in] Context Context to the function, which is the packet.\r
772db4bb 426\r
427 @retval TRUE This is the packet to cancel or its fragments.\r
428 @retval FALSE This is unrelated packet.\r
429\r
430**/\r
772db4bb 431BOOLEAN\r
432Ip4CancelPacketFragments (\r
96e1079f 433 IN IP4_LINK_TX_TOKEN *Frame,\r
434 IN VOID *Context\r
772db4bb 435 )\r
436{\r
437 if ((Frame->Packet == (NET_BUF *) Context) || (Frame->Context == Context)) {\r
438 return TRUE;\r
439 }\r
440\r
441 return FALSE;\r
442}\r
443\r
444\r
445/**\r
446 Cancel the Packet and all its fragments.\r
447\r
448 @param IpIf The interface from which the Packet is sent\r
449 @param Packet The Packet to cancel\r
450 @param IoStatus The status returns to the sender.\r
451\r
772db4bb 452**/\r
453VOID\r
454Ip4CancelPacket (\r
455 IN IP4_INTERFACE *IpIf,\r
456 IN NET_BUF *Packet,\r
457 IN EFI_STATUS IoStatus\r
458 )\r
459{\r
460 Ip4CancelFrames (IpIf, IoStatus, Ip4CancelPacketFragments, Packet);\r
461}\r