2 Transmit the IP4 packet.
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
15 Prepend an IP4 head to the Packet. It will copy the options and
16 build the IP4 header fields. Used for IP4 fragmentation.
18 @param Packet The packet to prepend IP4 header to
19 @param Head The caller supplied header. The caller should set
20 the following header fields: Tos, TotalLen, Id,
21 Fragment, Ttl, Protocol, Src and Dst. All the fields
22 are in host byte order. This function will fill in
23 the Ver, HeadLen, and checksum.
24 @param Option The orginal IP4 option to copy from
25 @param OptLen The length of the IP4 option
27 @retval EFI_BAD_BUFFER_SIZE There is no enought room in the head space of
29 @retval EFI_SUCCESS The IP4 header is successfully added to the packet.
34 IN OUT NET_BUF
*Packet
,
43 BOOLEAN FirstFragment
;
46 // Prepend the options: first get the option length, then copy it over.
49 FirstFragment
= IP4_FIRST_FRAGMENT (Head
->Fragment
);
51 Ip4CopyOption (Option
, OptLen
, FirstFragment
, NULL
, &Len
);
53 HeadLen
= IP4_MIN_HEADLEN
+ Len
;
54 ASSERT (((Len
% 4) == 0) && (HeadLen
<= IP4_MAX_HEADLEN
));
56 PacketHead
= (IP4_HEAD
*) NetbufAllocSpace (Packet
, HeadLen
, NET_BUF_HEAD
);
58 if (PacketHead
== NULL
) {
59 return EFI_BAD_BUFFER_SIZE
;
62 Ip4CopyOption (Option
, OptLen
, FirstFragment
, (UINT8
*) (PacketHead
+ 1), &Len
);
65 // Set the head up, convert the host byte order to network byte order
68 PacketHead
->HeadLen
= (UINT8
) (HeadLen
>> 2);
69 PacketHead
->Tos
= Head
->Tos
;
70 PacketHead
->TotalLen
= HTONS ((UINT16
) Packet
->TotalSize
);
71 PacketHead
->Id
= HTONS (Head
->Id
);
72 PacketHead
->Fragment
= HTONS (Head
->Fragment
);
73 PacketHead
->Checksum
= 0;
74 PacketHead
->Ttl
= Head
->Ttl
;
75 PacketHead
->Protocol
= Head
->Protocol
;
76 PacketHead
->Src
= HTONL (Head
->Src
);
77 PacketHead
->Dst
= HTONL (Head
->Dst
);
78 PacketHead
->Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) PacketHead
, HeadLen
));
80 Packet
->Ip
.Ip4
= PacketHead
;
86 Select an interface to send the packet generated in the IP4 driver
87 itself, that is, not by the requests of IP4 child's consumer. Such
88 packets include the ICMP echo replies, and other ICMP error packets.
90 @param[in] IpSb The IP4 service that wants to send the packets.
91 @param[in] Dst The destination of the packet
92 @param[in] Src The source of the packet
94 @return NULL if no proper interface is found, otherwise the interface that
95 can be used to send the system packet from.
100 IN IP4_SERVICE
*IpSb
,
106 IP4_INTERFACE
*Selected
;
110 // Select the interface the Dst is on if one of the connected
111 // network. Some IP instance may be configured with 0.0.0.0/0,
112 // don't select that interface now.
114 IpIf
= Ip4FindNet (IpSb
, Dst
);
116 if ((IpIf
!= NULL
) && (IpIf
->Ip
!= IP4_ALLZERO_ADDRESS
)) {
121 // If source is one of the interface address, select it.
123 IpIf
= Ip4FindInterface (IpSb
, Src
);
125 if ((IpIf
!= NULL
) && (IpIf
->Ip
!= IP4_ALLZERO_ADDRESS
)) {
130 // Select a configured interface as the fall back. Always prefer
131 // an interface with non-zero address.
135 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
136 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
138 if (IpIf
->Configured
&& ((Selected
== NULL
) || (Selected
->Ip
== 0))) {
148 The default callback function for system generated packet.
149 It will free the packet.
151 @param Ip4Instance The IP4 child that issued the transmission. It most
153 @param Packet The packet that transmitted.
154 @param IoStatus The result of the transmission, succeeded or failed.
155 @param LinkFlag Not used when transmission. check IP4_FRAME_CALLBACK
157 @param Context The context provided by us
162 IP4_PROTOCOL
*Ip4Instance
,
174 Transmit an IP4 packet. The packet comes either from the IP4
175 child's consumer (IpInstance != NULL) or the IP4 driver itself
176 (IpInstance == NULL). It will route the packet, fragment it,
177 then transmit all the fragments through some interface.
179 @param[in] IpSb The IP4 service instance to transmit the packet
180 @param[in] IpInstance The IP4 child that issues the transmission. It is
181 NULL if the packet is from the system.
182 @param[in] Packet The user data to send, excluding the IP header.
183 @param[in] Head The caller supplied header. The caller should set
184 the following header fields: Tos, TotalLen, Id, tl,
185 Fragment, Protocol, Src and Dst. All the fields are
186 in host byte order. This function will fill in the
187 Ver, HeadLen, Fragment, and checksum. The Fragment
188 only need to include the DF flag. Ip4Output will
189 compute the MF and offset for you.
190 @param[in] Option The original option to append to the IP headers
191 @param[in] OptLen The length of the option
192 @param[in] GateWay The next hop address to transmit packet to.
193 255.255.255.255 means broadcast.
194 @param[in] Callback The callback function to issue when transmission
196 @param[in] Context The opaque context for the callback
198 @retval EFI_NO_MAPPING There is no interface to the destination.
199 @retval EFI_NOT_FOUND There is no route to the destination
200 @retval EFI_SUCCESS The packet is successfully transmitted.
201 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length +
202 total data length is greater than MTU (or greater
203 than the maximum packet size if Token.Packet.TxData.
204 OverrideData.DoNotFragment is TRUE.)
205 @retval Others Failed to transmit the packet.
210 IN IP4_SERVICE
*IpSb
,
211 IN IP4_PROTOCOL
*IpInstance OPTIONAL
,
217 IN IP4_FRAME_CALLBACK Callback
,
222 IP4_ROUTE_CACHE_ENTRY
*CacheEntry
;
235 // Select an interface/source for system packet, application
236 // should select them itself.
238 if (IpInstance
== NULL
) {
239 IpIf
= Ip4SelectInterface (IpSb
, Head
->Dst
, Head
->Src
);
241 IpIf
= IpInstance
->Interface
;
245 return EFI_NO_MAPPING
;
248 if ((Head
->Src
== IP4_ALLZERO_ADDRESS
) && (IpInstance
== NULL
)) {
249 Head
->Src
= IpIf
->Ip
;
253 // Before IPsec process, prepared the IP head.
254 // If Ip4Output is transmitting RawData, don't update IPv4 header.
256 HeadLen
= sizeof (IP4_HEAD
) + ((OptLen
+ 3) & (~0x03));
258 if ((IpInstance
!= NULL
) && IpInstance
->ConfigData
.RawData
) {
261 Head
->HeadLen
= (UINT8
) (HeadLen
>> 2);
268 // Call IPsec process.
270 Status
= Ip4IpSecProcessPacket (
280 if (EFI_ERROR(Status
)) {
285 if (IP4_IS_BROADCAST (Ip4GetNetCast (Dest
, IpIf
)) || (Dest
== IP4_ALLONE_ADDRESS
)) {
287 // Set the gateway to local broadcast if the Dest is
288 // the broadcast address for the connected network or
289 // it is local broadcast.
291 GateWay
= IP4_ALLONE_ADDRESS
;
293 } else if (IP4_IS_MULTICAST (Dest
)) {
295 // Set the gateway to the destination if it is an multicast
296 // address. The IP4_INTERFACE won't consult ARP to send local
297 // broadcast and multicast.
301 } else if (GateWay
== IP4_ALLZERO_ADDRESS
) {
303 // Route the packet unless overrided, that is, GateWay isn't zero.
305 if (IpInstance
== NULL
) {
306 CacheEntry
= Ip4Route (IpSb
->DefaultRouteTable
, Head
->Dst
, Head
->Src
, IpIf
->SubnetMask
, TRUE
);
308 CacheEntry
= Ip4Route (IpInstance
->RouteTable
, Head
->Dst
, Head
->Src
, IpIf
->SubnetMask
, FALSE
);
310 // If failed to route the packet by using the instance's route table,
311 // try to use the default route table.
313 if (CacheEntry
== NULL
) {
314 CacheEntry
= Ip4Route (IpSb
->DefaultRouteTable
, Head
->Dst
, Head
->Src
, IpIf
->SubnetMask
, TRUE
);
318 if (CacheEntry
== NULL
) {
319 return EFI_NOT_FOUND
;
322 GateWay
= CacheEntry
->NextHop
;
323 Ip4FreeRouteCacheEntry (CacheEntry
);
327 // OK, selected the source and route, fragment the packet then send
328 // them. Tag each fragment other than the first one as spawn from it.
330 Mtu
= IpSb
->MaxPacketSize
+ sizeof (IP4_HEAD
);
332 if (Packet
->TotalSize
+ HeadLen
> Mtu
) {
334 // Fragmentation is diabled for RawData mode.
337 return EFI_BAD_BUFFER_SIZE
;
341 // Packet is fragmented from the tail to the head, that is, the
342 // first frame sent is the last fragment of the packet. The first
343 // fragment is NOT sent in this loop. First compute how many
344 // fragments there are.
346 Mtu
= (Mtu
- HeadLen
) & (~0x07);
347 Num
= (Packet
->TotalSize
+ Mtu
- 1) / Mtu
;
350 // Initialize the packet length and Offset. Other than the last
351 // fragment, the packet length equals to MTU. The offset is always
354 PacketLen
= Packet
->TotalSize
- (Num
- 1) * Mtu
;
355 Offset
= Mtu
* (Num
- 1);
357 for (Index
= 0; Index
< Num
- 1; Index
++, Offset
-= Mtu
) {
358 Fragment
= NetbufGetFragment (Packet
, Offset
, PacketLen
, IP4_MAX_HEADLEN
);
360 if (Fragment
== NULL
) {
361 Status
= EFI_OUT_OF_RESOURCES
;
366 // Update the header's fragment. The caller fills the IP4 header
367 // fields that are required by Ip4PrependHead except the fragment.
369 Head
->Fragment
= IP4_HEAD_FRAGMENT_FIELD (FALSE
, (Index
!= 0), Offset
);
370 Ip4PrependHead (Fragment
, Head
, Option
, OptLen
);
373 // Transmit the fragments, pass the Packet address as the context.
374 // So, we can find all the fragments spawned from the Packet by
375 // compare the NetBuf and Context to the Packet.
377 Status
= Ip4SendFrame (
387 if (EFI_ERROR (Status
)) {
395 // Trim the already sent data, then adjust the head's fragment field.
397 NetbufTrim (Packet
, Packet
->TotalSize
- Mtu
, FALSE
);
398 Head
->Fragment
= IP4_HEAD_FRAGMENT_FIELD (FALSE
, TRUE
, 0);
402 // Send the first fragment, it is either the orginal packet, or the
403 // first fragment of a fragmented packet. It seems that there is a subtle
404 // bug here: what if the caller free the packet in Callback and IpIf (or
405 // MNP child used by that interface) still holds the fragments and try
406 // to access the data? The caller can free the packet if it recycles the
407 // consumer's (such as UDP) data in the Callback. But this can't happen.
408 // The detailed sequence is:
409 // 1. for the packets generated by IP4 driver itself:
410 // The Callback is Ip4SysPacketSent, which is the same as the
411 // fragments' callback. Ip4SysPacketSent simply calls NetbufFree
412 // to release its reference to the packet. So, no problem for
415 // 2. for the upper layer's packets (use UDP as an example):
416 // UDP requests the IP layer to transmit some data which is
417 // wrapped in an asynchronous token, the token is wrapped
418 // in IP4_TXTOKEN_WRAP by IP4. IP4 also wrap the user's data
419 // in a net buffer, which is Packet we get here. IP4_TXTOKEN_WRAP
420 // is bound with the Packet. It will only be freed when all
421 // the references to Packet have been released. Upon then, the
422 // Packet's OnFree callback will release the IP4_TXTOKEN_WRAP,
423 // and singal the user's recycle event. So, also no problem for
424 // upper layer's packets.
426 Ip4PrependHead (Packet
, Head
, Option
, OptLen
);
427 Status
= Ip4SendFrame (IpIf
, IpInstance
, Packet
, GateWay
, Callback
, Context
, IpSb
);
429 if (EFI_ERROR (Status
)) {
436 Ip4CancelPacket (IpIf
, Packet
, Status
);
442 The filter function to find a packet and all its fragments.
443 The packet's fragments have their Context set to the packet.
445 @param[in] Frame The frames hold by the low level interface
446 @param[in] Context Context to the function, which is the packet.
448 @retval TRUE This is the packet to cancel or its fragments.
449 @retval FALSE This is unrelated packet.
453 Ip4CancelPacketFragments (
454 IN IP4_LINK_TX_TOKEN
*Frame
,
458 if ((Frame
->Packet
== (NET_BUF
*) Context
) || (Frame
->Context
== Context
)) {
467 Cancel the Packet and all its fragments.
469 @param IpIf The interface from which the Packet is sent
470 @param Packet The Packet to cancel
471 @param IoStatus The status returns to the sender.
476 IN IP4_INTERFACE
*IpIf
,
478 IN EFI_STATUS IoStatus
481 Ip4CancelFrames (IpIf
, IoStatus
, Ip4CancelPacketFragments
, Packet
);