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