Ip6Output.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /** @file
  2. The internal functions and routines to transmit the IP6 packet.
  3. Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Ip6Impl.h"
  7. UINT32 mIp6Id;
  8. /**
  9. Output all the available source addresses to a list entry head SourceList. The
  10. number of source addresses are also returned.
  11. @param[in] IpSb Points to an IP6 service binding instance.
  12. @param[out] SourceList The list entry head of all source addresses.
  13. It is the caller's responsibility to free the
  14. resources.
  15. @param[out] SourceCount The number of source addresses.
  16. @retval EFI_SUCCESS The source addresses were copied to a list entry head
  17. SourceList.
  18. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to complete the operation.
  19. **/
  20. EFI_STATUS
  21. Ip6CandidateSource (
  22. IN IP6_SERVICE *IpSb,
  23. OUT LIST_ENTRY *SourceList,
  24. OUT UINT32 *SourceCount
  25. )
  26. {
  27. IP6_INTERFACE *IpIf;
  28. LIST_ENTRY *Entry;
  29. LIST_ENTRY *Entry2;
  30. IP6_ADDRESS_INFO *AddrInfo;
  31. IP6_ADDRESS_INFO *Copy;
  32. *SourceCount = 0;
  33. if (IpSb->LinkLocalOk) {
  34. Copy = AllocatePool (sizeof (IP6_ADDRESS_INFO));
  35. if (Copy == NULL) {
  36. return EFI_OUT_OF_RESOURCES;
  37. }
  38. Copy->Signature = IP6_ADDR_INFO_SIGNATURE;
  39. IP6_COPY_ADDRESS (&Copy->Address, &IpSb->LinkLocalAddr);
  40. Copy->IsAnycast = FALSE;
  41. Copy->PrefixLength = IP6_LINK_LOCAL_PREFIX_LENGTH;
  42. Copy->ValidLifetime = (UINT32)IP6_INFINIT_LIFETIME;
  43. Copy->PreferredLifetime = (UINT32)IP6_INFINIT_LIFETIME;
  44. InsertTailList (SourceList, &Copy->Link);
  45. (*SourceCount)++;
  46. }
  47. NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
  48. IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
  49. NET_LIST_FOR_EACH (Entry2, &IpIf->AddressList) {
  50. AddrInfo = NET_LIST_USER_STRUCT_S (Entry2, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
  51. if (AddrInfo->IsAnycast) {
  52. //
  53. // Never use an anycast address.
  54. //
  55. continue;
  56. }
  57. Copy = AllocateCopyPool (sizeof (IP6_ADDRESS_INFO), AddrInfo);
  58. if (Copy == NULL) {
  59. return EFI_OUT_OF_RESOURCES;
  60. }
  61. InsertTailList (SourceList, &Copy->Link);
  62. (*SourceCount)++;
  63. }
  64. }
  65. return EFI_SUCCESS;
  66. }
  67. /**
  68. Calculate how many bits are the same between two IPv6 addresses.
  69. @param[in] AddressA Points to an IPv6 address.
  70. @param[in] AddressB Points to another IPv6 address.
  71. @return The common bits of the AddressA and AddressB.
  72. **/
  73. UINT8
  74. Ip6CommonPrefixLen (
  75. IN EFI_IPv6_ADDRESS *AddressA,
  76. IN EFI_IPv6_ADDRESS *AddressB
  77. )
  78. {
  79. UINT8 Count;
  80. UINT8 Index;
  81. UINT8 ByteA;
  82. UINT8 ByteB;
  83. UINT8 NumBits;
  84. Count = 0;
  85. Index = 0;
  86. while (Index < 16) {
  87. ByteA = AddressA->Addr[Index];
  88. ByteB = AddressB->Addr[Index];
  89. if (ByteA == ByteB) {
  90. Count += 8;
  91. Index++;
  92. continue;
  93. }
  94. //
  95. // Check how many bits are common between the two bytes.
  96. //
  97. NumBits = 8;
  98. ByteA = (UINT8)(ByteA ^ ByteB);
  99. while (ByteA != 0) {
  100. NumBits--;
  101. ByteA = (UINT8)(ByteA >> 1);
  102. }
  103. return (UINT8)(Count + NumBits);
  104. }
  105. return Count;
  106. }
  107. /**
  108. Output all the available source addresses to a list entry head SourceList. The
  109. number of source addresses are also returned.
  110. @param[in] IpSb Points to a IP6 service binding instance.
  111. @param[in] Destination The IPv6 destination address.
  112. @param[out] Source The selected IPv6 source address according to
  113. the Destination.
  114. @retval EFI_SUCCESS The source addresses were copied to a list entry
  115. head SourceList.
  116. @retval EFI_NO_MAPPING The IPv6 stack is not auto configured.
  117. **/
  118. EFI_STATUS
  119. Ip6SelectSourceAddress (
  120. IN IP6_SERVICE *IpSb,
  121. IN EFI_IPv6_ADDRESS *Destination,
  122. OUT EFI_IPv6_ADDRESS *Source
  123. )
  124. {
  125. EFI_STATUS Status;
  126. LIST_ENTRY SourceList;
  127. UINT32 SourceCount;
  128. UINT8 ScopeD;
  129. LIST_ENTRY *Entry;
  130. IP6_ADDRESS_INFO *AddrInfo;
  131. IP6_PREFIX_LIST_ENTRY *Prefix;
  132. UINT8 LastCommonLength;
  133. UINT8 CurrentCommonLength;
  134. EFI_IPv6_ADDRESS *TmpAddress;
  135. NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
  136. Status = EFI_SUCCESS;
  137. InitializeListHead (&SourceList);
  138. if (!IpSb->LinkLocalOk) {
  139. return EFI_NO_MAPPING;
  140. }
  141. //
  142. // Rule 1: Prefer same address.
  143. //
  144. if (Ip6IsOneOfSetAddress (IpSb, Destination, NULL, NULL)) {
  145. IP6_COPY_ADDRESS (Source, Destination);
  146. goto Exit;
  147. }
  148. //
  149. // Rule 2: Prefer appropriate scope.
  150. //
  151. if (IP6_IS_MULTICAST (Destination)) {
  152. ScopeD = (UINT8)(Destination->Addr[1] >> 4);
  153. } else if (NetIp6IsLinkLocalAddr (Destination)) {
  154. ScopeD = 0x2;
  155. } else {
  156. ScopeD = 0xE;
  157. }
  158. if (ScopeD <= 0x2) {
  159. //
  160. // Return the link-local address if it exists
  161. // One IP6_SERVICE only has one link-local address.
  162. //
  163. IP6_COPY_ADDRESS (Source, &IpSb->LinkLocalAddr);
  164. goto Exit;
  165. }
  166. //
  167. // All candidate source addresses are global unicast address.
  168. //
  169. Ip6CandidateSource (IpSb, &SourceList, &SourceCount);
  170. if (SourceCount == 0) {
  171. Status = EFI_NO_MAPPING;
  172. goto Exit;
  173. }
  174. IP6_COPY_ADDRESS (Source, &IpSb->LinkLocalAddr);
  175. if (SourceCount == 1) {
  176. goto Exit;
  177. }
  178. //
  179. // Rule 3: Avoid deprecated addresses.
  180. // TODO: check the "deprecated" state of the stateful configured address
  181. //
  182. NET_LIST_FOR_EACH (Entry, &IpSb->AutonomousPrefix) {
  183. Prefix = NET_LIST_USER_STRUCT (Entry, IP6_PREFIX_LIST_ENTRY, Link);
  184. if (Prefix->PreferredLifetime == 0) {
  185. Ip6RemoveAddr (NULL, &SourceList, &SourceCount, &Prefix->Prefix, Prefix->PrefixLength);
  186. if (SourceCount == 1) {
  187. goto Exit;
  188. }
  189. }
  190. }
  191. //
  192. // TODO: Rule 4: Prefer home addresses.
  193. // TODO: Rule 5: Prefer outgoing interface.
  194. // TODO: Rule 6: Prefer matching label.
  195. // TODO: Rule 7: Prefer public addresses.
  196. //
  197. //
  198. // Rule 8: Use longest matching prefix.
  199. //
  200. LastCommonLength = Ip6CommonPrefixLen (Source, Destination);
  201. TmpAddress = NULL;
  202. for (Entry = SourceList.ForwardLink; Entry != &SourceList; Entry = Entry->ForwardLink) {
  203. AddrInfo = NET_LIST_USER_STRUCT_S (Entry, IP6_ADDRESS_INFO, Link, IP6_ADDR_INFO_SIGNATURE);
  204. CurrentCommonLength = Ip6CommonPrefixLen (&AddrInfo->Address, Destination);
  205. if (CurrentCommonLength > LastCommonLength) {
  206. LastCommonLength = CurrentCommonLength;
  207. TmpAddress = &AddrInfo->Address;
  208. }
  209. }
  210. if (TmpAddress != NULL) {
  211. IP6_COPY_ADDRESS (Source, TmpAddress);
  212. }
  213. Exit:
  214. Ip6RemoveAddr (NULL, &SourceList, &SourceCount, NULL, 0);
  215. return Status;
  216. }
  217. /**
  218. Select an interface to send the packet generated in the IP6 driver
  219. itself: that is, not by the requests of the IP6 child's consumer. Such
  220. packets include the ICMPv6 echo replies and other ICMPv6 error packets.
  221. @param[in] IpSb The IP4 service that wants to send the packets.
  222. @param[in] Destination The destination of the packet.
  223. @param[in, out] Source The source of the packet.
  224. @return NULL if no proper interface is found, otherwise, the interface that
  225. can be used to send the system packet from.
  226. **/
  227. IP6_INTERFACE *
  228. Ip6SelectInterface (
  229. IN IP6_SERVICE *IpSb,
  230. IN EFI_IPv6_ADDRESS *Destination,
  231. IN OUT EFI_IPv6_ADDRESS *Source
  232. )
  233. {
  234. EFI_STATUS Status;
  235. EFI_IPv6_ADDRESS SelectedSource;
  236. IP6_INTERFACE *IpIf;
  237. BOOLEAN Exist;
  238. NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
  239. ASSERT (Destination != NULL && Source != NULL);
  240. if (NetIp6IsUnspecifiedAddr (Destination)) {
  241. return NULL;
  242. }
  243. if (!NetIp6IsUnspecifiedAddr (Source)) {
  244. Exist = Ip6IsOneOfSetAddress (IpSb, Source, &IpIf, NULL);
  245. ASSERT (Exist);
  246. return IpIf;
  247. }
  248. //
  249. // If source is unspecified, select a source according to the destination.
  250. //
  251. Status = Ip6SelectSourceAddress (IpSb, Destination, &SelectedSource);
  252. if (EFI_ERROR (Status)) {
  253. return IpSb->DefaultInterface;
  254. }
  255. Ip6IsOneOfSetAddress (IpSb, &SelectedSource, &IpIf, NULL);
  256. IP6_COPY_ADDRESS (Source, &SelectedSource);
  257. return IpIf;
  258. }
  259. /**
  260. The default callback function for the system generated packet.
  261. It will free the packet.
  262. @param[in] Packet The packet that transmitted.
  263. @param[in] IoStatus The result of the transmission, succeeded or failed.
  264. @param[in] LinkFlag Not used when transmitted. Check IP6_FRAME_CALLBACK
  265. for reference.
  266. @param[in] Context The context provided by us.
  267. **/
  268. VOID
  269. Ip6SysPacketSent (
  270. NET_BUF *Packet,
  271. EFI_STATUS IoStatus,
  272. UINT32 LinkFlag,
  273. VOID *Context
  274. )
  275. {
  276. NetbufFree (Packet);
  277. Packet = NULL;
  278. }
  279. /**
  280. Prefix an IP6 basic head and unfragmentable extension headers and a fragment header
  281. to the Packet. Used for IP6 fragmentation.
  282. @param[in] IpSb The IP6 service instance to transmit the packet.
  283. @param[in] Packet The packet to prefix the IP6 header to.
  284. @param[in] Head The caller supplied header.
  285. @param[in] FragmentOffset The fragment offset of the data following the header.
  286. @param[in] ExtHdrs The length of the original extension header.
  287. @param[in] ExtHdrsLen The length of the extension headers.
  288. @param[in] LastHeader The pointer of next header of last extension header.
  289. @param[in] HeadLen The length of the unfragmented part of the IP6 header.
  290. @retval EFI_BAD_BUFFER_SIZE There is no enough room in the head space of
  291. Packet.
  292. @retval EFI_SUCCESS The operation performed successfully.
  293. **/
  294. EFI_STATUS
  295. Ip6PrependHead (
  296. IN IP6_SERVICE *IpSb,
  297. IN NET_BUF *Packet,
  298. IN EFI_IP6_HEADER *Head,
  299. IN UINT16 FragmentOffset,
  300. IN UINT8 *ExtHdrs,
  301. IN UINT32 ExtHdrsLen,
  302. IN UINT8 LastHeader,
  303. IN UINT32 HeadLen
  304. )
  305. {
  306. UINT32 Len;
  307. UINT32 UnFragExtHdrsLen;
  308. EFI_IP6_HEADER *PacketHead;
  309. UINT8 *UpdatedExtHdrs;
  310. EFI_STATUS Status;
  311. UINT8 NextHeader;
  312. UpdatedExtHdrs = NULL;
  313. //
  314. // HeadLen is the length of the fixed part of the sequences of fragments, i.e.
  315. // the unfragment part.
  316. //
  317. PacketHead = (EFI_IP6_HEADER *)NetbufAllocSpace (Packet, HeadLen, NET_BUF_HEAD);
  318. if (PacketHead == NULL) {
  319. return EFI_BAD_BUFFER_SIZE;
  320. }
  321. //
  322. // Set the head up, convert the host byte order to network byte order
  323. //
  324. CopyMem (PacketHead, Head, sizeof (EFI_IP6_HEADER));
  325. PacketHead->PayloadLength = HTONS ((UINT16)(Packet->TotalSize - sizeof (EFI_IP6_HEADER)));
  326. Packet->Ip.Ip6 = PacketHead;
  327. Len = HeadLen - sizeof (EFI_IP6_HEADER);
  328. UnFragExtHdrsLen = Len - sizeof (IP6_FRAGMENT_HEADER);
  329. if (UnFragExtHdrsLen == 0) {
  330. PacketHead->NextHeader = IP6_FRAGMENT;
  331. }
  332. //
  333. // Append the extension headers: firstly copy the unfragmentable headers, then append
  334. // fragmentation header.
  335. //
  336. if ((FragmentOffset & IP6_FRAGMENT_OFFSET_MASK) == 0) {
  337. NextHeader = Head->NextHeader;
  338. } else {
  339. NextHeader = PacketHead->NextHeader;
  340. }
  341. Status = Ip6FillFragmentHeader (
  342. IpSb,
  343. NextHeader,
  344. LastHeader,
  345. ExtHdrs,
  346. ExtHdrsLen,
  347. FragmentOffset,
  348. &UpdatedExtHdrs
  349. );
  350. if (EFI_ERROR (Status)) {
  351. return Status;
  352. }
  353. CopyMem (
  354. (UINT8 *)(PacketHead + 1),
  355. UpdatedExtHdrs,
  356. UnFragExtHdrsLen + sizeof (IP6_FRAGMENT_HEADER)
  357. );
  358. FreePool (UpdatedExtHdrs);
  359. return EFI_SUCCESS;
  360. }
  361. /**
  362. Transmit an IP6 packet. The packet comes either from the IP6
  363. child's consumer (IpInstance != NULL) or the IP6 driver itself
  364. (IpInstance == NULL). It will route the packet, fragment it,
  365. then transmit all the fragments through an interface.
  366. @param[in] IpSb The IP6 service instance to transmit the packet.
  367. @param[in] Interface The IP6 interface to transmit the packet. Ignored
  368. if NULL.
  369. @param[in] IpInstance The IP6 child that issues the transmission. It is
  370. NULL if the packet is from the system.
  371. @param[in] Packet The user data to send, excluding the IP header.
  372. @param[in] Head The caller supplied header. The caller should set
  373. the following header fields: NextHeader, HopLimit,
  374. Src, Dest, FlowLabel, PayloadLength. This function
  375. will fill in the Ver, TrafficClass.
  376. @param[in] ExtHdrs The extension headers to append to the IPv6 basic
  377. header.
  378. @param[in] ExtHdrsLen The length of the extension headers.
  379. @param[in] Callback The callback function to issue when transmission
  380. completed.
  381. @param[in] Context The opaque context for the callback.
  382. @retval EFI_INVALID_PARAMETER Any input parameter or the packet is invalid.
  383. @retval EFI_NO_MAPPING There is no interface to the destination.
  384. @retval EFI_NOT_FOUND There is no route to the destination.
  385. @retval EFI_SUCCESS The packet successfully transmitted.
  386. @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lack of
  387. resources.
  388. @retval Others Failed to transmit the packet.
  389. **/
  390. EFI_STATUS
  391. Ip6Output (
  392. IN IP6_SERVICE *IpSb,
  393. IN IP6_INTERFACE *Interface OPTIONAL,
  394. IN IP6_PROTOCOL *IpInstance OPTIONAL,
  395. IN NET_BUF *Packet,
  396. IN EFI_IP6_HEADER *Head,
  397. IN UINT8 *ExtHdrs,
  398. IN UINT32 ExtHdrsLen,
  399. IN IP6_FRAME_CALLBACK Callback,
  400. IN VOID *Context
  401. )
  402. {
  403. IP6_INTERFACE *IpIf;
  404. EFI_IPv6_ADDRESS NextHop;
  405. IP6_NEIGHBOR_ENTRY *NeighborCache;
  406. IP6_ROUTE_CACHE_ENTRY *RouteCache;
  407. EFI_STATUS Status;
  408. UINT32 Mtu;
  409. UINT32 HeadLen;
  410. UINT16 FragmentOffset;
  411. UINT8 *LastHeader;
  412. UINT32 UnFragmentLen;
  413. UINT32 UnFragmentHdrsLen;
  414. UINT32 FragmentHdrsLen;
  415. UINT16 *Checksum;
  416. UINT16 PacketChecksum;
  417. UINT16 PseudoChecksum;
  418. UINT32 Index;
  419. UINT32 PacketLen;
  420. UINT32 RealExtLen;
  421. UINT32 Offset;
  422. NET_BUF *TmpPacket;
  423. NET_BUF *Fragment;
  424. UINT32 Num;
  425. UINT8 *Buf;
  426. EFI_IP6_HEADER *PacketHead;
  427. IP6_ICMP_HEAD *IcmpHead;
  428. IP6_TXTOKEN_WRAP *Wrap;
  429. IP6_ROUTE_ENTRY *RouteEntry;
  430. UINT8 *UpdatedExtHdrs;
  431. UINT8 NextHeader;
  432. UINT8 LastHeaderBackup;
  433. BOOLEAN FragmentHeadInserted;
  434. UINT8 *ExtHdrsBackup;
  435. UINT8 NextHeaderBackup;
  436. EFI_IPv6_ADDRESS Source;
  437. EFI_IPv6_ADDRESS Destination;
  438. NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
  439. //
  440. // RFC2460: Each extension header is an integer multiple of 8 octets long,
  441. // in order to retain 8-octet alignment for subsequent headers.
  442. //
  443. if ((ExtHdrsLen & 0x7) != 0) {
  444. return EFI_INVALID_PARAMETER;
  445. }
  446. LastHeader = NULL;
  447. Ip6IsExtsValid (
  448. NULL,
  449. NULL,
  450. &Head->NextHeader,
  451. ExtHdrs,
  452. ExtHdrsLen,
  453. FALSE,
  454. NULL,
  455. &LastHeader,
  456. NULL,
  457. NULL,
  458. NULL
  459. );
  460. //
  461. // Select an interface/source for system packet, application
  462. // should select them itself.
  463. //
  464. IpIf = Interface;
  465. if (IpIf == NULL) {
  466. //
  467. // IpInstance->Interface is NULL when IpInstance is configured with both stationaddress
  468. // and destinationaddress is unspecified.
  469. //
  470. if ((IpInstance == NULL) || (IpInstance->Interface == NULL)) {
  471. IpIf = Ip6SelectInterface (IpSb, &Head->DestinationAddress, &Head->SourceAddress);
  472. if (IpInstance != NULL) {
  473. IpInstance->Interface = IpIf;
  474. }
  475. } else {
  476. IpIf = IpInstance->Interface;
  477. }
  478. }
  479. if (IpIf == NULL) {
  480. return EFI_NO_MAPPING;
  481. }
  482. //
  483. // Update the common field in Head here.
  484. //
  485. Head->Version = 6;
  486. Head->TrafficClassL = 0;
  487. Head->TrafficClassH = 0;
  488. Checksum = NULL;
  489. NextHeader = *LastHeader;
  490. switch (NextHeader) {
  491. case EFI_IP_PROTO_UDP:
  492. Packet->Udp = (EFI_UDP_HEADER *)NetbufGetByte (Packet, 0, NULL);
  493. ASSERT (Packet->Udp != NULL);
  494. if (Packet->Udp->Checksum == 0) {
  495. Checksum = &Packet->Udp->Checksum;
  496. }
  497. break;
  498. case EFI_IP_PROTO_TCP:
  499. Packet->Tcp = (TCP_HEAD *)NetbufGetByte (Packet, 0, NULL);
  500. ASSERT (Packet->Tcp != NULL);
  501. if (Packet->Tcp->Checksum == 0) {
  502. Checksum = &Packet->Tcp->Checksum;
  503. }
  504. break;
  505. case IP6_ICMP:
  506. //
  507. // Don't send ICMP packet to an IPv6 anycast address.
  508. //
  509. if (Ip6IsAnycast (IpSb, &Head->DestinationAddress)) {
  510. return EFI_INVALID_PARAMETER;
  511. }
  512. IcmpHead = (IP6_ICMP_HEAD *)NetbufGetByte (Packet, 0, NULL);
  513. ASSERT (IcmpHead != NULL);
  514. if (IcmpHead->Checksum == 0) {
  515. Checksum = &IcmpHead->Checksum;
  516. }
  517. break;
  518. default:
  519. break;
  520. }
  521. if (Checksum != NULL) {
  522. //
  523. // Calculate the checksum for upper layer protocol if it is not calculated due to lack of
  524. // IPv6 source address.
  525. //
  526. PacketChecksum = NetbufChecksum (Packet);
  527. PseudoChecksum = NetIp6PseudoHeadChecksum (
  528. &Head->SourceAddress,
  529. &Head->DestinationAddress,
  530. NextHeader,
  531. Packet->TotalSize
  532. );
  533. *Checksum = (UINT16) ~NetAddChecksum (PacketChecksum, PseudoChecksum);
  534. }
  535. Status = Ip6IpSecProcessPacket (
  536. IpSb,
  537. &Head,
  538. LastHeader, // no need get the lasthead value for output
  539. &Packet,
  540. &ExtHdrs,
  541. &ExtHdrsLen,
  542. EfiIPsecOutBound,
  543. Context
  544. );
  545. if (EFI_ERROR (Status)) {
  546. return Status;
  547. }
  548. LastHeader = NULL;
  549. //
  550. // Check incoming parameters.
  551. //
  552. if (!Ip6IsExtsValid (
  553. IpSb,
  554. Packet,
  555. &Head->NextHeader,
  556. ExtHdrs,
  557. ExtHdrsLen,
  558. FALSE,
  559. NULL,
  560. &LastHeader,
  561. &RealExtLen,
  562. &UnFragmentHdrsLen,
  563. NULL
  564. ))
  565. {
  566. return EFI_INVALID_PARAMETER;
  567. }
  568. if ((RealExtLen & 0x7) != 0) {
  569. return EFI_INVALID_PARAMETER;
  570. }
  571. LastHeaderBackup = *LastHeader;
  572. //
  573. // Perform next hop determination:
  574. // For multicast packets, the next-hop is always the destination address and
  575. // is considered to be on-link.
  576. //
  577. if (IP6_IS_MULTICAST (&Head->DestinationAddress)) {
  578. IP6_COPY_ADDRESS (&NextHop, &Head->DestinationAddress);
  579. } else {
  580. //
  581. // For unicast packets, use a combination of the Destination Cache, the Prefix List
  582. // and the Default Router List to determine the IP address of the appropriate next hop.
  583. //
  584. NeighborCache = Ip6FindNeighborEntry (IpSb, &Head->DestinationAddress);
  585. if (NeighborCache != NULL) {
  586. //
  587. // Hit Neighbor Cache.
  588. //
  589. IP6_COPY_ADDRESS (&NextHop, &Head->DestinationAddress);
  590. } else {
  591. //
  592. // Not in Neighbor Cache, check Router cache
  593. //
  594. RouteCache = Ip6Route (IpSb, &Head->DestinationAddress, &Head->SourceAddress);
  595. if (RouteCache == NULL) {
  596. return EFI_NOT_FOUND;
  597. }
  598. IP6_COPY_ADDRESS (&NextHop, &RouteCache->NextHop);
  599. Ip6FreeRouteCacheEntry (RouteCache);
  600. }
  601. }
  602. //
  603. // Examines the Neighbor Cache for link-layer information about that neighbor.
  604. // DO NOT create neighbor cache if neighbor is itself - when reporting ICMP error.
  605. //
  606. if (!IP6_IS_MULTICAST (&NextHop) && !EFI_IP6_EQUAL (&Head->DestinationAddress, &Head->SourceAddress)) {
  607. NeighborCache = Ip6FindNeighborEntry (IpSb, &NextHop);
  608. if (NeighborCache == NULL) {
  609. NeighborCache = Ip6CreateNeighborEntry (IpSb, Ip6OnArpResolved, &NextHop, NULL);
  610. if (NeighborCache == NULL) {
  611. return EFI_OUT_OF_RESOURCES;
  612. }
  613. //
  614. // Send out multicast neighbor solicitation for address resolution immediately.
  615. //
  616. Ip6CreateSNMulticastAddr (&NeighborCache->Neighbor, &Destination);
  617. Status = Ip6SelectSourceAddress (IpSb, &NeighborCache->Neighbor, &Source);
  618. if (EFI_ERROR (Status)) {
  619. return Status;
  620. }
  621. Status = Ip6SendNeighborSolicit (
  622. IpSb,
  623. &Source,
  624. &Destination,
  625. &NeighborCache->Neighbor,
  626. &IpSb->SnpMode.CurrentAddress
  627. );
  628. if (EFI_ERROR (Status)) {
  629. return Status;
  630. }
  631. --NeighborCache->Transmit;
  632. NeighborCache->Ticks = IP6_GET_TICKS (IpSb->RetransTimer) + 1;
  633. }
  634. NeighborCache->Interface = IpIf;
  635. }
  636. UpdatedExtHdrs = NULL;
  637. ExtHdrsBackup = NULL;
  638. NextHeaderBackup = 0;
  639. FragmentHeadInserted = FALSE;
  640. //
  641. // Check whether we received Packet Too Big message for the packet sent to the
  642. // Destination. If yes include a Fragment Header in the subsequent packets.
  643. //
  644. RouteEntry = Ip6FindRouteEntry (
  645. IpSb->RouteTable,
  646. &Head->DestinationAddress,
  647. NULL
  648. );
  649. if (RouteEntry != NULL) {
  650. if ((RouteEntry->Flag & IP6_PACKET_TOO_BIG) == IP6_PACKET_TOO_BIG) {
  651. //
  652. // FragmentHead is inserted after Hop-by-Hop Options header, Destination
  653. // Options header (first occur), Routing header, and before Fragment header,
  654. // Authentication header, Encapsulating Security Payload header, and
  655. // Destination Options header (last occur), and upper-layer header.
  656. //
  657. Status = Ip6FillFragmentHeader (
  658. IpSb,
  659. Head->NextHeader,
  660. LastHeaderBackup,
  661. ExtHdrs,
  662. ExtHdrsLen,
  663. 0,
  664. &UpdatedExtHdrs
  665. );
  666. if (EFI_ERROR (Status)) {
  667. return Status;
  668. }
  669. if ((ExtHdrs == NULL) && (ExtHdrsLen == 0)) {
  670. NextHeaderBackup = Head->NextHeader;
  671. Head->NextHeader = IP6_FRAGMENT;
  672. }
  673. ExtHdrsBackup = ExtHdrs;
  674. ExtHdrs = UpdatedExtHdrs;
  675. ExtHdrsLen = ExtHdrsLen + sizeof (IP6_FRAGMENT_HEADER);
  676. RealExtLen = RealExtLen + sizeof (IP6_FRAGMENT_HEADER);
  677. mIp6Id++;
  678. FragmentHeadInserted = TRUE;
  679. }
  680. Ip6FreeRouteEntry (RouteEntry);
  681. }
  682. //
  683. // OK, selected the source and route, fragment the packet then send
  684. // them. Tag each fragment other than the first one as spawn from it.
  685. // Each extension header is an integer multiple of 8 octets long, in
  686. // order to retain 8-octet alignment for subsequent headers.
  687. //
  688. Mtu = IpSb->MaxPacketSize + sizeof (EFI_IP6_HEADER);
  689. HeadLen = sizeof (EFI_IP6_HEADER) + RealExtLen;
  690. if (Packet->TotalSize + HeadLen > Mtu) {
  691. //
  692. // Remove the inserted Fragment Header since we need fragment the packet.
  693. //
  694. if (FragmentHeadInserted) {
  695. ExtHdrs = ExtHdrsBackup;
  696. ExtHdrsLen = ExtHdrsLen - sizeof (IP6_FRAGMENT_HEADER);
  697. if ((ExtHdrs == NULL) && (ExtHdrsLen == 0)) {
  698. Head->NextHeader = NextHeaderBackup;
  699. }
  700. }
  701. FragmentHdrsLen = ExtHdrsLen - UnFragmentHdrsLen;
  702. //
  703. // The packet is beyond the maximum which can be described through the
  704. // fragment offset field in Fragment header.
  705. //
  706. if ((((Packet->TotalSize + FragmentHdrsLen) >> 3) & (~0x1fff)) != 0) {
  707. Status = EFI_BAD_BUFFER_SIZE;
  708. goto Error;
  709. }
  710. if (FragmentHdrsLen != 0) {
  711. //
  712. // Append the fragmentable extension hdrs before the upper layer payload
  713. // to form a new NET_BUF. This NET_BUF contains all the buffer which will
  714. // be fragmented below.
  715. //
  716. TmpPacket = NetbufGetFragment (Packet, 0, Packet->TotalSize, FragmentHdrsLen);
  717. ASSERT (TmpPacket != NULL);
  718. //
  719. // Allocate the space to contain the fragmentable hdrs and copy the data.
  720. //
  721. Buf = NetbufAllocSpace (TmpPacket, FragmentHdrsLen, TRUE);
  722. ASSERT (Buf != NULL);
  723. CopyMem (Buf, ExtHdrs + UnFragmentHdrsLen, FragmentHdrsLen);
  724. //
  725. // Free the old Packet.
  726. //
  727. NetbufFree (Packet);
  728. Packet = TmpPacket;
  729. }
  730. //
  731. // The unfragment part which appears in every fragmented IPv6 packet includes
  732. // the IPv6 header, the unfragmentable extension hdrs and the fragment header.
  733. //
  734. UnFragmentLen = sizeof (EFI_IP6_HEADER) + UnFragmentHdrsLen + sizeof (IP6_FRAGMENT_HEADER);
  735. //
  736. // Mtu now is the length of the fragment part in a full-length fragment.
  737. //
  738. Mtu = (Mtu - UnFragmentLen) & (~0x07);
  739. Num = (Packet->TotalSize + Mtu - 1) / Mtu;
  740. for (Index = 0, Offset = 0, PacketLen = Mtu; Index < Num; Index++) {
  741. //
  742. // Get fragment from the Packet, append UnFragmentLen spare buffer
  743. // before the fragmented data, the corresponding data is filled in later.
  744. //
  745. Fragment = NetbufGetFragment (Packet, Offset, PacketLen, UnFragmentLen);
  746. if (Fragment == NULL) {
  747. Status = EFI_OUT_OF_RESOURCES;
  748. goto Error;
  749. }
  750. FragmentOffset = (UINT16)((UINT16)Offset | 0x1);
  751. if (Index == Num - 1) {
  752. //
  753. // The last fragment, clear the M flag.
  754. //
  755. FragmentOffset &= (~0x1);
  756. }
  757. Status = Ip6PrependHead (
  758. IpSb,
  759. Fragment,
  760. Head,
  761. FragmentOffset,
  762. ExtHdrs,
  763. ExtHdrsLen,
  764. LastHeaderBackup,
  765. UnFragmentLen
  766. );
  767. ASSERT (Status == EFI_SUCCESS);
  768. Status = Ip6SendFrame (
  769. IpIf,
  770. IpInstance,
  771. Fragment,
  772. &NextHop,
  773. Ip6SysPacketSent,
  774. Packet
  775. );
  776. if (EFI_ERROR (Status)) {
  777. goto Error;
  778. }
  779. //
  780. // The last fragment of upper layer packet, update the IP6 token status.
  781. //
  782. if ((Index == Num -1) && (Context != NULL)) {
  783. Wrap = (IP6_TXTOKEN_WRAP *)Context;
  784. Wrap->Token->Status = Status;
  785. }
  786. Offset += PacketLen;
  787. PacketLen = Packet->TotalSize - Offset;
  788. if (PacketLen > Mtu) {
  789. PacketLen = Mtu;
  790. }
  791. }
  792. NetbufFree (Packet);
  793. mIp6Id++;
  794. if (UpdatedExtHdrs != NULL) {
  795. FreePool (UpdatedExtHdrs);
  796. }
  797. return EFI_SUCCESS;
  798. }
  799. //
  800. // Need not fragment the packet, send it in one frame.
  801. //
  802. PacketHead = (EFI_IP6_HEADER *)NetbufAllocSpace (Packet, HeadLen, NET_BUF_HEAD);
  803. if (PacketHead == NULL) {
  804. Status = EFI_BAD_BUFFER_SIZE;
  805. goto Error;
  806. }
  807. CopyMem (PacketHead, Head, sizeof (EFI_IP6_HEADER));
  808. Packet->Ip.Ip6 = PacketHead;
  809. if (ExtHdrs != NULL) {
  810. Buf = (UINT8 *)(PacketHead + 1);
  811. CopyMem (Buf, ExtHdrs, ExtHdrsLen);
  812. }
  813. if (UpdatedExtHdrs != NULL) {
  814. //
  815. // A Fragment Header is inserted to the packet, update the payload length.
  816. //
  817. PacketHead->PayloadLength = (UINT16)(NTOHS (PacketHead->PayloadLength) +
  818. sizeof (IP6_FRAGMENT_HEADER));
  819. PacketHead->PayloadLength = HTONS (PacketHead->PayloadLength);
  820. FreePool (UpdatedExtHdrs);
  821. }
  822. return Ip6SendFrame (
  823. IpIf,
  824. IpInstance,
  825. Packet,
  826. &NextHop,
  827. Callback,
  828. Context
  829. );
  830. Error:
  831. if (UpdatedExtHdrs != NULL) {
  832. FreePool (UpdatedExtHdrs);
  833. }
  834. Ip6CancelPacket (IpIf, Packet, Status);
  835. return Status;
  836. }
  837. /**
  838. The filter function to find a packet and all its fragments.
  839. The packet's fragments have their Context set to the packet.
  840. @param[in] Frame The frames hold by the low level interface.
  841. @param[in] Context Context to the function, which is the packet.
  842. @retval TRUE This is the packet to cancel or its fragments.
  843. @retval FALSE This is an unrelated packet.
  844. **/
  845. BOOLEAN
  846. Ip6CancelPacketFragments (
  847. IN IP6_LINK_TX_TOKEN *Frame,
  848. IN VOID *Context
  849. )
  850. {
  851. if ((Frame->Packet == (NET_BUF *)Context) || (Frame->Context == Context)) {
  852. return TRUE;
  853. }
  854. return FALSE;
  855. }
  856. /**
  857. Remove all the frames on the interface that pass the FrameToCancel,
  858. either queued on ARP queues or that have already been delivered to
  859. MNP and not yet recycled.
  860. @param[in] Interface Interface to remove the frames from.
  861. @param[in] IoStatus The transmit status returned to the frames' callback.
  862. @param[in] FrameToCancel Function to select the frame to cancel; NULL to select all.
  863. @param[in] Context Opaque parameters passed to FrameToCancel. Ignored if
  864. FrameToCancel is NULL.
  865. **/
  866. VOID
  867. Ip6CancelFrames (
  868. IN IP6_INTERFACE *Interface,
  869. IN EFI_STATUS IoStatus,
  870. IN IP6_FRAME_TO_CANCEL FrameToCancel OPTIONAL,
  871. IN VOID *Context OPTIONAL
  872. )
  873. {
  874. LIST_ENTRY *Entry;
  875. LIST_ENTRY *Next;
  876. IP6_LINK_TX_TOKEN *Token;
  877. IP6_SERVICE *IpSb;
  878. IP6_NEIGHBOR_ENTRY *ArpQue;
  879. EFI_STATUS Status;
  880. IpSb = Interface->Service;
  881. NET_CHECK_SIGNATURE (IpSb, IP6_SERVICE_SIGNATURE);
  882. //
  883. // Cancel all the pending frames on ARP requests
  884. //
  885. NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->ArpQues) {
  886. ArpQue = NET_LIST_USER_STRUCT (Entry, IP6_NEIGHBOR_ENTRY, ArpList);
  887. Status = Ip6FreeNeighborEntry (
  888. IpSb,
  889. ArpQue,
  890. FALSE,
  891. FALSE,
  892. IoStatus,
  893. FrameToCancel,
  894. Context
  895. );
  896. ASSERT_EFI_ERROR (Status);
  897. }
  898. //
  899. // Cancel all the frames that have been delivered to MNP
  900. // but not yet recycled.
  901. //
  902. NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->SentFrames) {
  903. Token = NET_LIST_USER_STRUCT (Entry, IP6_LINK_TX_TOKEN, Link);
  904. if ((FrameToCancel == NULL) || FrameToCancel (Token, Context)) {
  905. IpSb->Mnp->Cancel (IpSb->Mnp, &Token->MnpToken);
  906. }
  907. }
  908. }
  909. /**
  910. Cancel the Packet and all its fragments.
  911. @param[in] IpIf The interface from which the Packet is sent.
  912. @param[in] Packet The Packet to cancel.
  913. @param[in] IoStatus The status returns to the sender.
  914. **/
  915. VOID
  916. Ip6CancelPacket (
  917. IN IP6_INTERFACE *IpIf,
  918. IN NET_BUF *Packet,
  919. IN EFI_STATUS IoStatus
  920. )
  921. {
  922. Ip6CancelFrames (IpIf, IoStatus, Ip6CancelPacketFragments, Packet);
  923. }