2 Transmit the IP4 packet.
4 Copyright (c) 2005 - 2009, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 Prepend an IP4 head to the Packet. It will copy the options and
22 build the IP4 header fields. Used for IP4 fragmentation.
24 @param Packet The packet to prepend IP4 header to
25 @param Head The caller supplied header. The caller should set
26 the following header fields: Tos, TotalLen, Id,
27 Fragment, Ttl, Protocol, Src and Dst. All the fields
28 are in host byte order. This function will fill in
29 the Ver, HeadLen, and checksum.
30 @param Option The orginal IP4 option to copy from
31 @param OptLen The length of the IP4 option
33 @retval EFI_BAD_BUFFER_SIZE There is no enought room in the head space of
35 @retval EFI_SUCCESS The IP4 header is successfully added to the packet.
40 IN OUT NET_BUF
*Packet
,
49 BOOLEAN FirstFragment
;
52 // Prepend the options: first get the option length, then copy it over.
55 FirstFragment
= IP4_FIRST_FRAGMENT (Head
->Fragment
);
57 Ip4CopyOption (Option
, OptLen
, FirstFragment
, NULL
, &Len
);
59 HeadLen
= IP4_MIN_HEADLEN
+ Len
;
60 ASSERT (((Len
% 4) == 0) && (HeadLen
<= IP4_MAX_HEADLEN
));
62 PacketHead
= (IP4_HEAD
*) NetbufAllocSpace (Packet
, HeadLen
, NET_BUF_HEAD
);
64 if (PacketHead
== NULL
) {
65 return EFI_BAD_BUFFER_SIZE
;
68 Ip4CopyOption (Option
, OptLen
, FirstFragment
, (UINT8
*) (PacketHead
+ 1), &Len
);
71 // Set the head up, convert the host byte order to network byte order
74 PacketHead
->HeadLen
= (UINT8
) (HeadLen
>> 2);
75 PacketHead
->Tos
= Head
->Tos
;
76 PacketHead
->TotalLen
= HTONS ((UINT16
) Packet
->TotalSize
);
77 PacketHead
->Id
= HTONS (Head
->Id
);
78 PacketHead
->Fragment
= HTONS (Head
->Fragment
);
79 PacketHead
->Checksum
= 0;
80 PacketHead
->Ttl
= Head
->Ttl
;
81 PacketHead
->Protocol
= Head
->Protocol
;
82 PacketHead
->Src
= HTONL (Head
->Src
);
83 PacketHead
->Dst
= HTONL (Head
->Dst
);
84 PacketHead
->Checksum
= (UINT16
) (~NetblockChecksum ((UINT8
*) PacketHead
, HeadLen
));
86 Packet
->Ip
.Ip4
= PacketHead
;
92 Select an interface to send the packet generated in the IP4 driver
93 itself, that is, not by the requests of IP4 child's consumer. Such
94 packets include the ICMP echo replies, and other ICMP error packets.
96 @param[in] IpSb The IP4 service that wants to send the packets.
97 @param[in] Dst The destination of the packet
98 @param[in] Src The source of the packet
100 @return NULL if no proper interface is found, otherwise the interface that
101 can be used to send the system packet from.
106 IN IP4_SERVICE
*IpSb
,
112 IP4_INTERFACE
*Selected
;
116 // Select the interface the Dst is on if one of the connected
117 // network. Some IP instance may be configured with 0.0.0.0/0,
118 // don't select that interface now.
120 IpIf
= Ip4FindNet (IpSb
, Dst
);
122 if ((IpIf
!= NULL
) && (IpIf
->Ip
!= IP4_ALLZERO_ADDRESS
)) {
127 // If source is one of the interface address, select it.
129 IpIf
= Ip4FindInterface (IpSb
, Src
);
131 if ((IpIf
!= NULL
) && (IpIf
->Ip
!= IP4_ALLZERO_ADDRESS
)) {
136 // Select a configured interface as the fall back. Always prefer
137 // an interface with non-zero address.
141 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
142 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
144 if (IpIf
->Configured
&& ((Selected
== NULL
) || (Selected
->Ip
== 0))) {
154 The default callback function for system generated packet.
155 It will free the packet.
157 @param Ip4Instance The IP4 child that issued the transmission. It most
159 @param Packet The packet that transmitted.
160 @param IoStatus The result of the transmission, succeeded or failed.
161 @param LinkFlag Not used when transmission. check IP4_FRAME_CALLBACK
163 @param Context The context provided by us
168 IP4_PROTOCOL
*Ip4Instance
,
180 Transmit an IP4 packet. The packet comes either from the IP4
181 child's consumer (IpInstance != NULL) or the IP4 driver itself
182 (IpInstance == NULL). It will route the packet, fragment it,
183 then transmit all the fragments through some interface.
185 @param[in] IpSb The IP4 service instance to transmit the packet
186 @param[in] IpInstance The IP4 child that issues the transmission. It is
187 NULL if the packet is from the system.
188 @param[in] Packet The user data to send, excluding the IP header.
189 @param[in] Head The caller supplied header. The caller should set
190 the following header fields: Tos, TotalLen, Id, tl,
191 Fragment, Protocol, Src and Dst. All the fields are
192 in host byte order. This function will fill in the
193 Ver, HeadLen, Fragment, and checksum. The Fragment
194 only need to include the DF flag. Ip4Output will
195 compute the MF and offset for you.
196 @param[in] Option The original option to append to the IP headers
197 @param[in] OptLen The length of the option
198 @param[in] GateWay The next hop address to transmit packet to.
199 255.255.255.255 means broadcast.
200 @param[in] Callback The callback function to issue when transmission
202 @param[in] Context The opaque context for the callback
204 @retval EFI_NO_MAPPING There is no interface to the destination.
205 @retval EFI_NOT_FOUND There is no route to the destination
206 @retval EFI_SUCCESS The packet is successfully transmitted.
207 @retval Others Failed to transmit the packet.
212 IN IP4_SERVICE
*IpSb
,
213 IN IP4_PROTOCOL
*IpInstance OPTIONAL
,
219 IN IP4_FRAME_CALLBACK Callback
,
224 IP4_ROUTE_CACHE_ENTRY
*CacheEntry
;
236 // Select an interface/source for system packet, application
237 // should select them itself.
239 if (IpInstance
== NULL
) {
240 IpIf
= Ip4SelectInterface (IpSb
, Head
->Dst
, Head
->Src
);
242 IpIf
= IpInstance
->Interface
;
246 return EFI_NO_MAPPING
;
249 if ((Head
->Src
== IP4_ALLZERO_ADDRESS
) && (IpInstance
== NULL
)) {
250 Head
->Src
= IpIf
->Ip
;
254 // Route the packet unless overrided, that is, GateWay isn't zero.
256 if (GateWay
== IP4_ALLZERO_ADDRESS
) {
259 if (IP4_IS_BROADCAST (Ip4GetNetCast (Dest
, IpIf
)) || (Dest
== IP4_ALLONE_ADDRESS
)) {
261 // Set the gateway to local broadcast if the Dest is
262 // the broadcast address for the connected network or
263 // it is local broadcast.
265 GateWay
= IP4_ALLONE_ADDRESS
;
267 } else if (IP4_IS_MULTICAST (Dest
)) {
269 // Set the gateway to the destination if it is an multicast
270 // address. The IP4_INTERFACE won't consult ARP to send local
271 // broadcast and multicast.
277 // Consult the route table to route the packet
279 if (IpInstance
== NULL
) {
280 CacheEntry
= Ip4Route (IpSb
->DefaultRouteTable
, Head
->Dst
, Head
->Src
);
282 CacheEntry
= Ip4Route (IpInstance
->RouteTable
, Head
->Dst
, Head
->Src
);
285 if (CacheEntry
== NULL
) {
286 return EFI_NOT_FOUND
;
289 GateWay
= CacheEntry
->NextHop
;
290 Ip4FreeRouteCacheEntry (CacheEntry
);
295 // TODO: currently Option/OptLen are not included into encryption scope.
297 Status
= Ip4IpSecProcessPacket (
307 if (EFI_ERROR(Status
)) {
312 // OK, selected the source and route, fragment the packet then send
313 // them. Tag each fragment other than the first one as spawn from it.
315 Mtu
= IpSb
->MaxPacketSize
;
316 HeadLen
= sizeof (IP4_HEAD
) + ((OptLen
+ 3) & (~0x03));
319 if (Packet
->TotalSize
+ HeadLen
> Mtu
) {
321 // Packet is fragmented from the tail to the head, that is, the
322 // first frame sent is the last fragment of the packet. The first
323 // fragment is NOT sent in this loop. First compute how many
324 // fragments there are.
326 Mtu
= (Mtu
- HeadLen
) & (~0x07);
327 Num
= (Packet
->TotalSize
+ Mtu
- 1) / Mtu
;
330 // Initialize the packet length and Offset. Other than the last
331 // fragment, the packet length equals to MTU. The offset is always
334 PacketLen
= Packet
->TotalSize
- (Num
- 1) * Mtu
;
335 Offset
= Mtu
* (Num
- 1);
337 for (Index
= 0; Index
< Num
- 1; Index
++, Offset
-= Mtu
) {
338 Fragment
= NetbufGetFragment (Packet
, Offset
, PacketLen
, IP4_MAX_HEADLEN
);
340 if (Fragment
== NULL
) {
341 Status
= EFI_OUT_OF_RESOURCES
;
346 // Update the header's fragment. The caller fills the IP4 header
347 // fields that are required by Ip4PrependHead except the fragment.
349 Head
->Fragment
= IP4_HEAD_FRAGMENT_FIELD (FALSE
, (Index
!= 0), Offset
);
350 Ip4PrependHead (Fragment
, Head
, Option
, OptLen
);
353 // Transmit the fragments, pass the Packet address as the context.
354 // So, we can find all the fragments spawned from the Packet by
355 // compare the NetBuf and Context to the Packet.
357 Status
= Ip4SendFrame (
366 if (EFI_ERROR (Status
)) {
374 // Trim the already sent data, then adjust the head's fragment field.
376 NetbufTrim (Packet
, Packet
->TotalSize
- Mtu
, FALSE
);
377 Head
->Fragment
= IP4_HEAD_FRAGMENT_FIELD (FALSE
, TRUE
, 0);
381 // Send the first fragment, it is either the orginal packet, or the
382 // first fragment of a fragmented packet. It seems that there is a subtle
383 // bug here: what if the caller free the packet in Callback and IpIf (or
384 // MNP child used by that interface) still holds the fragments and try
385 // to access the data? The caller can free the packet if it recycles the
386 // consumer's (such as UDP) data in the Callback. But this can't happen.
387 // The detailed sequence is:
388 // 1. for the packets generated by IP4 driver itself:
389 // The Callback is Ip4SysPacketSent, which is the same as the
390 // fragments' callback. Ip4SysPacketSent simply calls NetbufFree
391 // to release its reference to the packet. So, no problem for
394 // 2. for the upper layer's packets (use UDP as an example):
395 // UDP requests the IP layer to transmit some data which is
396 // wrapped in an asynchronous token, the token is wrapped
397 // in IP4_TXTOKEN_WRAP by IP4. IP4 also wrap the user's data
398 // in a net buffer, which is Packet we get here. IP4_TXTOKEN_WRAP
399 // is bound with the Packet. It will only be freed when all
400 // the references to Packet have been released. Upon then, the
401 // Packet's OnFree callback will release the IP4_TXTOKEN_WRAP,
402 // and singal the user's recycle event. So, also no problem for
403 // upper layer's packets.
405 Ip4PrependHead (Packet
, Head
, Option
, OptLen
);
406 Status
= Ip4SendFrame (IpIf
, IpInstance
, Packet
, GateWay
, Callback
, Context
);
408 if (EFI_ERROR (Status
)) {
415 Ip4CancelPacket (IpIf
, Packet
, Status
);
421 The filter function to find a packet and all its fragments.
422 The packet's fragments have their Context set to the packet.
424 @param[in] Frame The frames hold by the low level interface
425 @param[in] Context Context to the function, which is the packet.
427 @retval TRUE This is the packet to cancel or its fragments.
428 @retval FALSE This is unrelated packet.
432 Ip4CancelPacketFragments (
433 IN IP4_LINK_TX_TOKEN
*Frame
,
437 if ((Frame
->Packet
== (NET_BUF
*) Context
) || (Frame
->Context
== Context
)) {
446 Cancel the Packet and all its fragments.
448 @param IpIf The interface from which the Packet is sent
449 @param Packet The Packet to cancel
450 @param IoStatus The status returns to the sender.
455 IN IP4_INTERFACE
*IpIf
,
457 IN EFI_STATUS IoStatus
460 Ip4CancelFrames (IpIf
, IoStatus
, Ip4CancelPacketFragments
, Packet
);