Ip6Option.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /** @file
  2. IP6 option support functions and routines.
  3. Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Ip6Impl.h"
  7. /**
  8. Validate the IP6 option format for both the packets we received
  9. and that we will transmit. It will compute the ICMPv6 error message fields
  10. if the option is malformatted.
  11. @param[in] IpSb The IP6 service data.
  12. @param[in] Packet The to be validated packet.
  13. @param[in] Option The first byte of the option.
  14. @param[in] OptionLen The length of the whole option.
  15. @param[in] Pointer Identifies the octet offset within
  16. the invoking packet where the error was detected.
  17. @retval TRUE The option is properly formatted.
  18. @retval FALSE The option is malformatted.
  19. **/
  20. BOOLEAN
  21. Ip6IsOptionValid (
  22. IN IP6_SERVICE *IpSb,
  23. IN NET_BUF *Packet,
  24. IN UINT8 *Option,
  25. IN UINT8 OptionLen,
  26. IN UINT32 Pointer
  27. )
  28. {
  29. UINT8 Offset;
  30. UINT8 OptionType;
  31. Offset = 0;
  32. while (Offset < OptionLen) {
  33. OptionType = *(Option + Offset);
  34. switch (OptionType) {
  35. case Ip6OptionPad1:
  36. //
  37. // It is a Pad1 option
  38. //
  39. Offset++;
  40. break;
  41. case Ip6OptionPadN:
  42. //
  43. // It is a PadN option
  44. //
  45. Offset = (UINT8)(Offset + *(Option + Offset + 1) + 2);
  46. break;
  47. case Ip6OptionRouterAlert:
  48. //
  49. // It is a Router Alert Option
  50. //
  51. Offset += 4;
  52. break;
  53. default:
  54. //
  55. // The highest-order two bits specify the action must be taken if
  56. // the processing IPv6 node does not recognize the option type.
  57. //
  58. switch (OptionType & Ip6OptionMask) {
  59. case Ip6OptionSkip:
  60. Offset = (UINT8)(Offset + *(Option + Offset + 1));
  61. break;
  62. case Ip6OptionDiscard:
  63. return FALSE;
  64. case Ip6OptionParameterProblem:
  65. Pointer = Pointer + Offset + sizeof (EFI_IP6_HEADER);
  66. Ip6SendIcmpError (
  67. IpSb,
  68. Packet,
  69. NULL,
  70. &Packet->Ip.Ip6->SourceAddress,
  71. ICMP_V6_PARAMETER_PROBLEM,
  72. 2,
  73. &Pointer
  74. );
  75. return FALSE;
  76. case Ip6OptionMask:
  77. if (!IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress)) {
  78. Pointer = Pointer + Offset + sizeof (EFI_IP6_HEADER);
  79. Ip6SendIcmpError (
  80. IpSb,
  81. Packet,
  82. NULL,
  83. &Packet->Ip.Ip6->SourceAddress,
  84. ICMP_V6_PARAMETER_PROBLEM,
  85. 2,
  86. &Pointer
  87. );
  88. }
  89. return FALSE;
  90. break;
  91. }
  92. break;
  93. }
  94. }
  95. return TRUE;
  96. }
  97. /**
  98. Validate the IP6 option format for both the packets we received
  99. and that we will transmit. It supports the defined options in Neighbor
  100. Discovery messages.
  101. @param[in] Option The first byte of the option.
  102. @param[in] OptionLen The length of the whole option.
  103. @retval TRUE The option is properly formatted.
  104. @retval FALSE The option is malformatted.
  105. **/
  106. BOOLEAN
  107. Ip6IsNDOptionValid (
  108. IN UINT8 *Option,
  109. IN UINT16 OptionLen
  110. )
  111. {
  112. UINT32 Offset;
  113. UINT16 Length;
  114. IP6_OPTION_HEADER *OptionHeader;
  115. if (Option == NULL) {
  116. ASSERT (Option != NULL);
  117. return FALSE;
  118. }
  119. Offset = 0;
  120. //
  121. // RFC 4861 states that Neighbor Discovery packet can contain zero or more
  122. // options. Start processing the options if at least Type + Length fields
  123. // fit within the input buffer.
  124. //
  125. while (Offset + sizeof (IP6_OPTION_HEADER) - 1 < OptionLen) {
  126. OptionHeader = (IP6_OPTION_HEADER *)(Option + Offset);
  127. Length = (UINT16)OptionHeader->Length * 8;
  128. switch (OptionHeader->Type) {
  129. case Ip6OptionPrefixInfo:
  130. if (Length != 32) {
  131. return FALSE;
  132. }
  133. break;
  134. case Ip6OptionMtu:
  135. if (Length != 8) {
  136. return FALSE;
  137. }
  138. break;
  139. default:
  140. // RFC 4861 states that Length field cannot be 0.
  141. if (Length == 0) {
  142. return FALSE;
  143. }
  144. break;
  145. }
  146. //
  147. // Check whether recognized options are within the input buffer's scope.
  148. //
  149. switch (OptionHeader->Type) {
  150. case Ip6OptionEtherSource:
  151. case Ip6OptionEtherTarget:
  152. case Ip6OptionPrefixInfo:
  153. case Ip6OptionRedirected:
  154. case Ip6OptionMtu:
  155. if (Offset + Length > (UINT32)OptionLen) {
  156. return FALSE;
  157. }
  158. break;
  159. default:
  160. //
  161. // Unrecognized options can be either valid (but unused) or invalid
  162. // (garbage in between or right after valid options). Silently ignore.
  163. //
  164. break;
  165. }
  166. //
  167. // Advance to the next option.
  168. // Length already considers option header's Type + Length.
  169. //
  170. Offset += Length;
  171. }
  172. return TRUE;
  173. }
  174. /**
  175. Validate whether the NextHeader is a known valid protocol or one of the user configured
  176. protocols from the upper layer.
  177. @param[in] IpSb The IP6 service instance.
  178. @param[in] NextHeader The next header field.
  179. @retval TRUE The NextHeader is a known valid protocol or user configured.
  180. @retval FALSE The NextHeader is not a known valid protocol.
  181. **/
  182. BOOLEAN
  183. Ip6IsValidProtocol (
  184. IN IP6_SERVICE *IpSb,
  185. IN UINT8 NextHeader
  186. )
  187. {
  188. LIST_ENTRY *Entry;
  189. IP6_PROTOCOL *IpInstance;
  190. if ((NextHeader == EFI_IP_PROTO_TCP) ||
  191. (NextHeader == EFI_IP_PROTO_UDP) ||
  192. (NextHeader == IP6_ICMP) ||
  193. (NextHeader == IP6_ESP)
  194. )
  195. {
  196. return TRUE;
  197. }
  198. if (IpSb == NULL) {
  199. return FALSE;
  200. }
  201. if (IpSb->Signature != IP6_SERVICE_SIGNATURE) {
  202. return FALSE;
  203. }
  204. NET_LIST_FOR_EACH (Entry, &IpSb->Children) {
  205. IpInstance = NET_LIST_USER_STRUCT_S (Entry, IP6_PROTOCOL, Link, IP6_PROTOCOL_SIGNATURE);
  206. if (IpInstance->State == IP6_STATE_CONFIGED) {
  207. if (IpInstance->ConfigData.DefaultProtocol == NextHeader) {
  208. return TRUE;
  209. }
  210. }
  211. }
  212. return FALSE;
  213. }
  214. /**
  215. Validate the IP6 extension header format for both the packets we received
  216. and that we will transmit. It will compute the ICMPv6 error message fields
  217. if the option is mal-formatted.
  218. @param[in] IpSb The IP6 service instance. This is an optional parameter.
  219. @param[in] Packet The data of the packet. Ignored if NULL.
  220. @param[in] NextHeader The next header field in IPv6 basic header.
  221. @param[in] ExtHdrs The first byte of the option.
  222. @param[in] ExtHdrsLen The length of the whole option.
  223. @param[in] Rcvd The option is from the packet we received if TRUE,
  224. otherwise, the option we want to transmit.
  225. @param[out] FormerHeader The offset of NextHeader which points to Fragment
  226. Header when we received, of the ExtHdrs.
  227. Ignored if we transmit.
  228. @param[out] LastHeader The pointer of NextHeader of the last extension
  229. header processed by IP6.
  230. @param[out] RealExtsLen The length of extension headers processed by IP6 layer.
  231. This is an optional parameter that may be NULL.
  232. @param[out] UnFragmentLen The length of unfragmented length of extension headers.
  233. This is an optional parameter that may be NULL.
  234. @param[out] Fragmented Indicate whether the packet is fragmented.
  235. This is an optional parameter that may be NULL.
  236. @retval TRUE The option is properly formatted.
  237. @retval FALSE The option is malformatted.
  238. **/
  239. BOOLEAN
  240. Ip6IsExtsValid (
  241. IN IP6_SERVICE *IpSb OPTIONAL,
  242. IN NET_BUF *Packet OPTIONAL,
  243. IN UINT8 *NextHeader,
  244. IN UINT8 *ExtHdrs,
  245. IN UINT32 ExtHdrsLen,
  246. IN BOOLEAN Rcvd,
  247. OUT UINT32 *FormerHeader OPTIONAL,
  248. OUT UINT8 **LastHeader,
  249. OUT UINT32 *RealExtsLen OPTIONAL,
  250. OUT UINT32 *UnFragmentLen OPTIONAL,
  251. OUT BOOLEAN *Fragmented OPTIONAL
  252. )
  253. {
  254. UINT32 Pointer;
  255. UINT32 Offset;
  256. UINT8 *Option;
  257. UINT8 OptionLen;
  258. BOOLEAN Flag;
  259. UINT8 CountD;
  260. UINT8 CountA;
  261. IP6_FRAGMENT_HEADER *FragmentHead;
  262. UINT16 FragmentOffset;
  263. IP6_ROUTING_HEADER *RoutingHead;
  264. if (RealExtsLen != NULL) {
  265. *RealExtsLen = 0;
  266. }
  267. if (UnFragmentLen != NULL) {
  268. *UnFragmentLen = 0;
  269. }
  270. if (Fragmented != NULL) {
  271. *Fragmented = FALSE;
  272. }
  273. *LastHeader = NextHeader;
  274. if ((ExtHdrs == NULL) && (ExtHdrsLen == 0)) {
  275. return TRUE;
  276. }
  277. if (((ExtHdrs == NULL) && (ExtHdrsLen != 0)) || ((ExtHdrs != NULL) && (ExtHdrsLen == 0))) {
  278. return FALSE;
  279. }
  280. Pointer = 0;
  281. Offset = 0;
  282. Flag = FALSE;
  283. CountD = 0;
  284. CountA = 0;
  285. while (Offset <= ExtHdrsLen) {
  286. switch (*NextHeader) {
  287. case IP6_HOP_BY_HOP:
  288. if (Offset != 0) {
  289. if (!Rcvd) {
  290. return FALSE;
  291. }
  292. //
  293. // Hop-by-Hop Options header is restricted to appear immediately after an IPv6 header only.
  294. // If not, generate a ICMP parameter problem message with code value of 1.
  295. //
  296. if (Pointer == 0) {
  297. Pointer = sizeof (EFI_IP6_HEADER);
  298. } else {
  299. Pointer = Offset + sizeof (EFI_IP6_HEADER);
  300. }
  301. if ((IpSb != NULL) && (Packet != NULL) &&
  302. !IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress))
  303. {
  304. Ip6SendIcmpError (
  305. IpSb,
  306. Packet,
  307. NULL,
  308. &Packet->Ip.Ip6->SourceAddress,
  309. ICMP_V6_PARAMETER_PROBLEM,
  310. 1,
  311. &Pointer
  312. );
  313. }
  314. return FALSE;
  315. }
  316. Flag = TRUE;
  317. //
  318. // Fall through
  319. //
  320. case IP6_DESTINATION:
  321. if (*NextHeader == IP6_DESTINATION) {
  322. CountD++;
  323. }
  324. if (CountD > 2) {
  325. return FALSE;
  326. }
  327. NextHeader = ExtHdrs + Offset;
  328. Pointer = Offset;
  329. Offset++;
  330. Option = ExtHdrs + Offset;
  331. OptionLen = (UINT8)((*Option + 1) * 8 - 2);
  332. Option++;
  333. Offset++;
  334. if ((IpSb != NULL) && (Packet != NULL) && !Ip6IsOptionValid (IpSb, Packet, Option, OptionLen, Offset)) {
  335. return FALSE;
  336. }
  337. Offset = Offset + OptionLen;
  338. if (Flag) {
  339. if (UnFragmentLen != NULL) {
  340. *UnFragmentLen = Offset;
  341. }
  342. Flag = FALSE;
  343. }
  344. break;
  345. case IP6_ROUTING:
  346. NextHeader = ExtHdrs + Offset;
  347. RoutingHead = (IP6_ROUTING_HEADER *)NextHeader;
  348. //
  349. // Type 0 routing header is defined in RFC2460 and deprecated in RFC5095.
  350. // Thus all routing types are processed as unrecognized.
  351. //
  352. if (RoutingHead->SegmentsLeft == 0) {
  353. //
  354. // Ignore the routing header and proceed to process the next header.
  355. //
  356. Offset = Offset + (RoutingHead->HeaderLen + 1) * 8;
  357. if (UnFragmentLen != NULL) {
  358. *UnFragmentLen = Offset;
  359. }
  360. } else {
  361. //
  362. // Discard the packet and send an ICMP Parameter Problem, Code 0, message
  363. // to the packet's source address, pointing to the unrecognized routing
  364. // type.
  365. //
  366. Pointer = Offset + 2 + sizeof (EFI_IP6_HEADER);
  367. if ((IpSb != NULL) && (Packet != NULL) &&
  368. !IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress))
  369. {
  370. Ip6SendIcmpError (
  371. IpSb,
  372. Packet,
  373. NULL,
  374. &Packet->Ip.Ip6->SourceAddress,
  375. ICMP_V6_PARAMETER_PROBLEM,
  376. 0,
  377. &Pointer
  378. );
  379. }
  380. return FALSE;
  381. }
  382. break;
  383. case IP6_FRAGMENT:
  384. //
  385. // RFC2402, AH header should after fragment header.
  386. //
  387. if (CountA > 1) {
  388. return FALSE;
  389. }
  390. //
  391. // RFC2460, ICMP Parameter Problem message with code 0 should be sent
  392. // if the length of a fragment is not a multiple of 8 octets and the M
  393. // flag of that fragment is 1, pointing to the Payload length field of the
  394. // fragment packet.
  395. //
  396. if ((IpSb != NULL) && (Packet != NULL) && ((ExtHdrsLen % 8) != 0)) {
  397. //
  398. // Check whether it is the last fragment.
  399. //
  400. FragmentHead = (IP6_FRAGMENT_HEADER *)(ExtHdrs + Offset);
  401. if (FragmentHead == NULL) {
  402. return FALSE;
  403. }
  404. FragmentOffset = NTOHS (FragmentHead->FragmentOffset);
  405. if (((FragmentOffset & 0x1) == 0x1) &&
  406. !IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress))
  407. {
  408. Pointer = sizeof (UINT32);
  409. Ip6SendIcmpError (
  410. IpSb,
  411. Packet,
  412. NULL,
  413. &Packet->Ip.Ip6->SourceAddress,
  414. ICMP_V6_PARAMETER_PROBLEM,
  415. 0,
  416. &Pointer
  417. );
  418. return FALSE;
  419. }
  420. }
  421. if (Fragmented != NULL) {
  422. *Fragmented = TRUE;
  423. }
  424. if (Rcvd && (FormerHeader != NULL)) {
  425. *FormerHeader = (UINT32)(NextHeader - ExtHdrs);
  426. }
  427. NextHeader = ExtHdrs + Offset;
  428. Offset = Offset + 8;
  429. break;
  430. case IP6_AH:
  431. if (++CountA > 1) {
  432. return FALSE;
  433. }
  434. Option = ExtHdrs + Offset;
  435. NextHeader = Option;
  436. Option++;
  437. //
  438. // RFC2402, Payload length is specified in 32-bit words, minus "2".
  439. //
  440. OptionLen = (UINT8)((*Option + 2) * 4);
  441. Offset = Offset + OptionLen;
  442. break;
  443. case IP6_NO_NEXT_HEADER:
  444. *LastHeader = NextHeader;
  445. return FALSE;
  446. break;
  447. default:
  448. if (Ip6IsValidProtocol (IpSb, *NextHeader)) {
  449. *LastHeader = NextHeader;
  450. if (RealExtsLen != NULL) {
  451. *RealExtsLen = Offset;
  452. }
  453. return TRUE;
  454. }
  455. //
  456. // The Next Header value is unrecognized by the node, discard the packet and
  457. // send an ICMP parameter problem message with code value of 1.
  458. //
  459. if (Offset == 0) {
  460. //
  461. // The Next Header directly follows IPv6 basic header.
  462. //
  463. Pointer = 6;
  464. } else {
  465. if (Pointer == 0) {
  466. Pointer = sizeof (EFI_IP6_HEADER);
  467. } else {
  468. Pointer = Offset + sizeof (EFI_IP6_HEADER);
  469. }
  470. }
  471. if ((IpSb != NULL) && (Packet != NULL) &&
  472. !IP6_IS_MULTICAST (&Packet->Ip.Ip6->DestinationAddress))
  473. {
  474. Ip6SendIcmpError (
  475. IpSb,
  476. Packet,
  477. NULL,
  478. &Packet->Ip.Ip6->SourceAddress,
  479. ICMP_V6_PARAMETER_PROBLEM,
  480. 1,
  481. &Pointer
  482. );
  483. }
  484. return FALSE;
  485. }
  486. }
  487. *LastHeader = NextHeader;
  488. if (RealExtsLen != NULL) {
  489. *RealExtsLen = Offset;
  490. }
  491. return TRUE;
  492. }
  493. /**
  494. Generate an IPv6 router alert option in network order and output it through Buffer.
  495. @param[out] Buffer Points to a buffer to record the generated option.
  496. @param[in, out] BufferLen The length of Buffer, in bytes.
  497. @param[in] NextHeader The 8-bit selector indicates the type of header
  498. immediately following the Hop-by-Hop Options header.
  499. @retval EFI_BUFFER_TOO_SMALL The Buffer is too small to contain the generated
  500. option. BufferLen is updated for the required size.
  501. @retval EFI_SUCCESS The option is generated and filled in to Buffer.
  502. **/
  503. EFI_STATUS
  504. Ip6FillHopByHop (
  505. OUT UINT8 *Buffer,
  506. IN OUT UINTN *BufferLen,
  507. IN UINT8 NextHeader
  508. )
  509. {
  510. UINT8 BufferArray[8];
  511. if (*BufferLen < 8) {
  512. *BufferLen = 8;
  513. return EFI_BUFFER_TOO_SMALL;
  514. }
  515. //
  516. // Form the Hop-By-Hop option in network order.
  517. // NextHeader (1 octet) + HdrExtLen (1 octet) + RouterAlertOption(4 octets) + PadN
  518. // The Hdr Ext Len is the length in 8-octet units, and does not including the first 8 octets.
  519. //
  520. ZeroMem (BufferArray, sizeof (BufferArray));
  521. BufferArray[0] = NextHeader;
  522. BufferArray[2] = 0x5;
  523. BufferArray[3] = 0x2;
  524. BufferArray[6] = 1;
  525. CopyMem (Buffer, BufferArray, sizeof (BufferArray));
  526. return EFI_SUCCESS;
  527. }
  528. /**
  529. Insert a Fragment Header to the Extension headers and output it in UpdatedExtHdrs.
  530. @param[in] IpSb The IP6 service instance to transmit the packet.
  531. @param[in] NextHeader The extension header type of first extension header.
  532. @param[in] LastHeader The extension header type of last extension header.
  533. @param[in] ExtHdrs The length of the original extension header.
  534. @param[in] ExtHdrsLen The length of the extension headers.
  535. @param[in] FragmentOffset The fragment offset of the data following the header.
  536. @param[out] UpdatedExtHdrs The updated ExtHdrs with Fragment header inserted.
  537. It's caller's responsibility to free this buffer.
  538. @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lake of
  539. resource.
  540. @retval EFI_UNSUPPORTED The extension header specified in ExtHdrs is not
  541. supported currently.
  542. @retval EFI_SUCCESS The operation performed successfully.
  543. **/
  544. EFI_STATUS
  545. Ip6FillFragmentHeader (
  546. IN IP6_SERVICE *IpSb,
  547. IN UINT8 NextHeader,
  548. IN UINT8 LastHeader,
  549. IN UINT8 *ExtHdrs,
  550. IN UINT32 ExtHdrsLen,
  551. IN UINT16 FragmentOffset,
  552. OUT UINT8 **UpdatedExtHdrs
  553. )
  554. {
  555. UINT32 Length;
  556. UINT8 *Buffer;
  557. UINT32 FormerHeader;
  558. UINT32 Offset;
  559. UINT32 Part1Len;
  560. UINT32 HeaderLen;
  561. UINT8 Current;
  562. IP6_FRAGMENT_HEADER FragmentHead;
  563. if (UpdatedExtHdrs == NULL) {
  564. return EFI_INVALID_PARAMETER;
  565. }
  566. Length = ExtHdrsLen + sizeof (IP6_FRAGMENT_HEADER);
  567. Buffer = AllocatePool (Length);
  568. if (Buffer == NULL) {
  569. return EFI_OUT_OF_RESOURCES;
  570. }
  571. Offset = 0;
  572. Part1Len = 0;
  573. FormerHeader = 0;
  574. Current = NextHeader;
  575. while ((ExtHdrs != NULL) && (Offset <= ExtHdrsLen)) {
  576. switch (NextHeader) {
  577. case IP6_ROUTING:
  578. case IP6_HOP_BY_HOP:
  579. case IP6_DESTINATION:
  580. Current = NextHeader;
  581. NextHeader = *(ExtHdrs + Offset);
  582. if ((Current == IP6_DESTINATION) && (NextHeader != IP6_ROUTING)) {
  583. //
  584. // Destination Options header should occur at most twice, once before
  585. // a Routing header and once before the upper-layer header. Here we
  586. // find the one before the upper-layer header. Insert the Fragment
  587. // Header before it.
  588. //
  589. CopyMem (Buffer, ExtHdrs, Part1Len);
  590. *(Buffer + FormerHeader) = IP6_FRAGMENT;
  591. //
  592. // Exit the loop.
  593. //
  594. Offset = ExtHdrsLen + 1;
  595. break;
  596. }
  597. FormerHeader = Offset;
  598. HeaderLen = (*(ExtHdrs + Offset + 1) + 1) * 8;
  599. Part1Len = Part1Len + HeaderLen;
  600. Offset = Offset + HeaderLen;
  601. break;
  602. case IP6_FRAGMENT:
  603. Current = NextHeader;
  604. if (Part1Len != 0) {
  605. CopyMem (Buffer, ExtHdrs, Part1Len);
  606. }
  607. *(Buffer + FormerHeader) = IP6_FRAGMENT;
  608. //
  609. // Exit the loop.
  610. //
  611. Offset = ExtHdrsLen + 1;
  612. break;
  613. case IP6_AH:
  614. Current = NextHeader;
  615. NextHeader = *(ExtHdrs + Offset);
  616. //
  617. // RFC2402, Payload length is specified in 32-bit words, minus "2".
  618. //
  619. HeaderLen = (*(ExtHdrs + Offset + 1) + 2) * 4;
  620. Part1Len = Part1Len + HeaderLen;
  621. Offset = Offset + HeaderLen;
  622. break;
  623. default:
  624. if (Ip6IsValidProtocol (IpSb, NextHeader)) {
  625. Current = NextHeader;
  626. CopyMem (Buffer, ExtHdrs, Part1Len);
  627. *(Buffer + FormerHeader) = IP6_FRAGMENT;
  628. //
  629. // Exit the loop.
  630. //
  631. Offset = ExtHdrsLen + 1;
  632. break;
  633. }
  634. FreePool (Buffer);
  635. return EFI_UNSUPPORTED;
  636. }
  637. }
  638. //
  639. // Append the Fragment header. If the fragment offset indicates the fragment
  640. // is the first fragment.
  641. //
  642. if ((FragmentOffset & IP6_FRAGMENT_OFFSET_MASK) == 0) {
  643. FragmentHead.NextHeader = Current;
  644. } else {
  645. FragmentHead.NextHeader = LastHeader;
  646. }
  647. FragmentHead.Reserved = 0;
  648. FragmentHead.FragmentOffset = HTONS (FragmentOffset);
  649. FragmentHead.Identification = mIp6Id;
  650. CopyMem (Buffer + Part1Len, &FragmentHead, sizeof (IP6_FRAGMENT_HEADER));
  651. if ((ExtHdrs != NULL) && (Part1Len < ExtHdrsLen)) {
  652. //
  653. // Append the part2 (fragmentable part) of Extension headers
  654. //
  655. CopyMem (
  656. Buffer + Part1Len + sizeof (IP6_FRAGMENT_HEADER),
  657. ExtHdrs + Part1Len,
  658. ExtHdrsLen - Part1Len
  659. );
  660. }
  661. *UpdatedExtHdrs = Buffer;
  662. return EFI_SUCCESS;
  663. }