1 /** @file
2   Transmit the IP4 packet.
3 
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include "Ip4Impl.h"
10 
11 UINT16  mIp4Id;
12 
13 
14 /**
15   Prepend an IP4 head to the Packet. It will copy the options and
16   build the IP4 header fields. Used for IP4 fragmentation.
17 
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
26 
27   @retval EFI_BAD_BUFFER_SIZE  There is no enought room in the head space of
28                                Packet.
29   @retval EFI_SUCCESS          The IP4 header is successfully added to the packet.
30 
31 **/
32 EFI_STATUS
Ip4PrependHead(IN OUT NET_BUF * Packet,IN IP4_HEAD * Head,IN UINT8 * Option,IN UINT32 OptLen)33 Ip4PrependHead (
34   IN OUT NET_BUF                *Packet,
35   IN     IP4_HEAD               *Head,
36   IN     UINT8                  *Option,
37   IN     UINT32                 OptLen
38   )
39 {
40   UINT32                    HeadLen;
41   UINT32                    Len;
42   IP4_HEAD                  *PacketHead;
43   BOOLEAN                   FirstFragment;
44 
45   //
46   // Prepend the options: first get the option length, then copy it over.
47   //
48   HeadLen       = 0;
49   FirstFragment = IP4_FIRST_FRAGMENT (Head->Fragment);
50 
51   Ip4CopyOption (Option, OptLen, FirstFragment, NULL, &Len);
52 
53   HeadLen = IP4_MIN_HEADLEN + Len;
54   ASSERT (((Len % 4) == 0) && (HeadLen <= IP4_MAX_HEADLEN));
55 
56   PacketHead = (IP4_HEAD *) NetbufAllocSpace (Packet, HeadLen, NET_BUF_HEAD);
57 
58   if (PacketHead == NULL) {
59     return EFI_BAD_BUFFER_SIZE;
60   }
61 
62   Ip4CopyOption (Option, OptLen, FirstFragment, (UINT8 *) (PacketHead + 1), &Len);
63 
64   //
65   // Set the head up, convert the host byte order to network byte order
66   //
67   PacketHead->Ver       = 4;
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));
79 
80   Packet->Ip.Ip4        = PacketHead;
81   return EFI_SUCCESS;
82 }
83 
84 
85 /**
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.
89 
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
93 
94   @return NULL if no proper interface is found, otherwise the interface that
95           can be used to send the system packet from.
96 
97 **/
98 IP4_INTERFACE *
Ip4SelectInterface(IN IP4_SERVICE * IpSb,IN IP4_ADDR Dst,IN IP4_ADDR Src)99 Ip4SelectInterface (
100   IN IP4_SERVICE            *IpSb,
101   IN IP4_ADDR               Dst,
102   IN IP4_ADDR               Src
103   )
104 {
105   IP4_INTERFACE             *IpIf;
106   IP4_INTERFACE             *Selected;
107   LIST_ENTRY                *Entry;
108 
109   //
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.
113   //
114   IpIf = Ip4FindNet (IpSb, Dst);
115 
116   if ((IpIf != NULL) && (IpIf->Ip != IP4_ALLZERO_ADDRESS)) {
117     return IpIf;
118   }
119 
120   //
121   // If source is one of the interface address, select it.
122   //
123   IpIf = Ip4FindInterface (IpSb, Src);
124 
125   if ((IpIf != NULL) && (IpIf->Ip != IP4_ALLZERO_ADDRESS)) {
126     return IpIf;
127   }
128 
129   //
130   // Select a configured interface as the fall back. Always prefer
131   // an interface with non-zero address.
132   //
133   Selected = NULL;
134 
135   NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
136     IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
137 
138     if (IpIf->Configured && ((Selected == NULL) || (Selected->Ip == 0))) {
139       Selected = IpIf;
140     }
141   }
142 
143   return Selected;
144 }
145 
146 
147 /**
148   The default callback function for system generated packet.
149   It will free the packet.
150 
151   @param  Ip4Instance          The IP4 child that issued the transmission.  It most
152                                like is NULL.
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
156                                for reference.
157   @param  Context              The context provided by us
158 
159 **/
160 VOID
Ip4SysPacketSent(IP4_PROTOCOL * Ip4Instance,NET_BUF * Packet,EFI_STATUS IoStatus,UINT32 LinkFlag,VOID * Context)161 Ip4SysPacketSent (
162   IP4_PROTOCOL              *Ip4Instance,
163   NET_BUF                   *Packet,
164   EFI_STATUS                IoStatus,
165   UINT32                    LinkFlag,
166   VOID                      *Context
167   )
168 {
169   NetbufFree (Packet);
170 }
171 
172 
173 /**
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.
178 
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
195                                completed.
196   @param[in]  Context          The opaque context for the callback
197 
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.
206 
207 **/
208 EFI_STATUS
Ip4Output(IN IP4_SERVICE * IpSb,IN IP4_PROTOCOL * IpInstance OPTIONAL,IN NET_BUF * Packet,IN IP4_HEAD * Head,IN UINT8 * Option,IN UINT32 OptLen,IN IP4_ADDR GateWay,IN IP4_FRAME_CALLBACK Callback,IN VOID * Context)209 Ip4Output (
210   IN IP4_SERVICE            *IpSb,
211   IN IP4_PROTOCOL           *IpInstance  OPTIONAL,
212   IN NET_BUF                *Packet,
213   IN IP4_HEAD               *Head,
214   IN UINT8                  *Option,
215   IN UINT32                 OptLen,
216   IN IP4_ADDR               GateWay,
217   IN IP4_FRAME_CALLBACK     Callback,
218   IN VOID                   *Context
219   )
220 {
221   IP4_INTERFACE             *IpIf;
222   IP4_ROUTE_CACHE_ENTRY     *CacheEntry;
223   IP4_ADDR                  Dest;
224   EFI_STATUS                Status;
225   NET_BUF                   *Fragment;
226   UINT32                    Index;
227   UINT32                    HeadLen;
228   UINT32                    PacketLen;
229   UINT32                    Offset;
230   UINT32                    Mtu;
231   UINT32                    Num;
232   BOOLEAN                   RawData;
233 
234   //
235   // Select an interface/source for system packet, application
236   // should select them itself.
237   //
238   if (IpInstance == NULL) {
239     IpIf = Ip4SelectInterface (IpSb, Head->Dst, Head->Src);
240   } else {
241     IpIf = IpInstance->Interface;
242   }
243 
244   if (IpIf == NULL) {
245     return EFI_NO_MAPPING;
246   }
247 
248   if ((Head->Src == IP4_ALLZERO_ADDRESS) && (IpInstance == NULL)) {
249     Head->Src = IpIf->Ip;
250   }
251 
252   //
253   // Before IPsec process, prepared the IP head.
254   // If Ip4Output is transmitting RawData, don't update IPv4 header.
255   //
256   HeadLen = sizeof (IP4_HEAD) + ((OptLen + 3) & (~0x03));
257 
258   if ((IpInstance != NULL) && IpInstance->ConfigData.RawData) {
259     RawData        = TRUE;
260   } else {
261     Head->HeadLen  = (UINT8) (HeadLen >> 2);
262     Head->Id       = mIp4Id++;
263     Head->Ver      = 4;
264     RawData        = FALSE;
265   }
266 
267   //
268   // Call IPsec process.
269   //
270   Status = Ip4IpSecProcessPacket (
271              IpSb,
272              &Head,
273              &Packet,
274              &Option,
275              &OptLen,
276              EfiIPsecOutBound,
277              Context
278              );
279 
280   if (EFI_ERROR(Status)) {
281     return Status;
282   }
283 
284   Dest = Head->Dst;
285   if (IP4_IS_BROADCAST (Ip4GetNetCast (Dest, IpIf)) || (Dest == IP4_ALLONE_ADDRESS)) {
286     //
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.
290     //
291     GateWay = IP4_ALLONE_ADDRESS;
292 
293   } else if (IP4_IS_MULTICAST (Dest)) {
294     //
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.
298     //
299     GateWay = Head->Dst;
300 
301   } else if (GateWay == IP4_ALLZERO_ADDRESS) {
302     //
303     // Route the packet unless overrided, that is, GateWay isn't zero.
304     //
305     if (IpInstance == NULL) {
306       CacheEntry = Ip4Route (IpSb->DefaultRouteTable, Head->Dst, Head->Src, IpIf->SubnetMask, TRUE);
307     } else {
308       CacheEntry = Ip4Route (IpInstance->RouteTable, Head->Dst, Head->Src, IpIf->SubnetMask, FALSE);
309       //
310       // If failed to route the packet by using the instance's route table,
311       // try to use the default route table.
312       //
313       if (CacheEntry == NULL) {
314         CacheEntry = Ip4Route (IpSb->DefaultRouteTable, Head->Dst, Head->Src, IpIf->SubnetMask, TRUE);
315       }
316     }
317 
318     if (CacheEntry == NULL) {
319       return EFI_NOT_FOUND;
320     }
321 
322     GateWay = CacheEntry->NextHop;
323     Ip4FreeRouteCacheEntry (CacheEntry);
324   }
325 
326   //
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.
329   //
330   Mtu = IpSb->MaxPacketSize + sizeof (IP4_HEAD);
331 
332   if (Packet->TotalSize + HeadLen > Mtu) {
333     //
334     // Fragmentation is diabled for RawData mode.
335     //
336     if (RawData) {
337       return EFI_BAD_BUFFER_SIZE;
338     }
339 
340     //
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.
345     //
346     Mtu       = (Mtu - HeadLen) & (~0x07);
347     Num       = (Packet->TotalSize + Mtu - 1) / Mtu;
348 
349     //
350     // Initialize the packet length and Offset. Other than the last
351     // fragment, the packet length equals to MTU. The offset is always
352     // aligned to MTU.
353     //
354     PacketLen = Packet->TotalSize - (Num - 1) * Mtu;
355     Offset    = Mtu * (Num - 1);
356 
357     for (Index = 0; Index < Num - 1; Index++, Offset -= Mtu) {
358       Fragment = NetbufGetFragment (Packet, Offset, PacketLen, IP4_MAX_HEADLEN);
359 
360       if (Fragment == NULL) {
361         Status = EFI_OUT_OF_RESOURCES;
362         goto ON_ERROR;
363       }
364 
365       //
366       // Update the header's fragment. The caller fills the IP4 header
367       // fields that are required by Ip4PrependHead except the fragment.
368       //
369       Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, (Index != 0), Offset);
370       Ip4PrependHead (Fragment, Head, Option, OptLen);
371 
372       //
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.
376       //
377       Status = Ip4SendFrame (
378                  IpIf,
379                  IpInstance,
380                  Fragment,
381                  GateWay,
382                  Ip4SysPacketSent,
383                  Packet,
384                  IpSb
385                  );
386 
387       if (EFI_ERROR (Status)) {
388         goto ON_ERROR;
389       }
390 
391       PacketLen = Mtu;
392     }
393 
394     //
395     // Trim the already sent data, then adjust the head's fragment field.
396     //
397     NetbufTrim (Packet, Packet->TotalSize - Mtu, FALSE);
398     Head->Fragment = IP4_HEAD_FRAGMENT_FIELD (FALSE, TRUE, 0);
399   }
400 
401   //
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
413   //    system packets.
414   //
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.
425   //
426   Ip4PrependHead (Packet, Head, Option, OptLen);
427   Status = Ip4SendFrame (IpIf, IpInstance, Packet, GateWay, Callback, Context, IpSb);
428 
429   if (EFI_ERROR (Status)) {
430     goto ON_ERROR;
431   }
432 
433   return EFI_SUCCESS;
434 
435 ON_ERROR:
436   Ip4CancelPacket (IpIf, Packet, Status);
437   return Status;
438 }
439 
440 
441 /**
442   The filter function to find a packet and all its fragments.
443   The packet's fragments have their Context set to the packet.
444 
445   @param[in]  Frame            The frames hold by the low level interface
446   @param[in]  Context          Context to the function, which is the packet.
447 
448   @retval TRUE                 This is the packet to cancel or its fragments.
449   @retval FALSE                This is unrelated packet.
450 
451 **/
452 BOOLEAN
Ip4CancelPacketFragments(IN IP4_LINK_TX_TOKEN * Frame,IN VOID * Context)453 Ip4CancelPacketFragments (
454   IN IP4_LINK_TX_TOKEN   *Frame,
455   IN VOID                *Context
456   )
457 {
458   if ((Frame->Packet == (NET_BUF *) Context) || (Frame->Context == Context)) {
459     return TRUE;
460   }
461 
462   return FALSE;
463 }
464 
465 
466 /**
467   Cancel the Packet and all its fragments.
468 
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.
472 
473 **/
474 VOID
Ip4CancelPacket(IN IP4_INTERFACE * IpIf,IN NET_BUF * Packet,IN EFI_STATUS IoStatus)475 Ip4CancelPacket (
476   IN IP4_INTERFACE    *IpIf,
477   IN NET_BUF          *Packet,
478   IN EFI_STATUS       IoStatus
479   )
480 {
481   Ip4CancelFrames (IpIf, IoStatus, Ip4CancelPacketFragments, Packet);
482 }
483