Repository navigation
Expand file tree
/
Copy pathicmp.go
More file actions
1658 lines (1583 loc) · 63.6 KB
/
Copy pathicmp.go
File metadata and controls
1658 lines (1583 loc) · 63.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mipstack
import (
"encoding/binary"
"fmt"
"net/netip"
"runtime"
"syscall"
)
const (
// ICMPCodeNone is the zero code used by message types without subcodes.
ICMPCodeNone = 0
// ICMPv4TypeEchoReply is the ICMPv4 Echo Reply type.
ICMPv4TypeEchoReply = 0
// ICMPv4TypeDestinationUnreachable is the ICMPv4 Destination Unreachable type.
ICMPv4TypeDestinationUnreachable = 3
// ICMPv4TypeEchoRequest is the ICMPv4 Echo Request type.
ICMPv4TypeEchoRequest = 8
// ICMPv4TypeTimeExceeded is the ICMPv4 Time Exceeded type.
ICMPv4TypeTimeExceeded = 11
// ICMPv4TypeParameterProblem is the ICMPv4 Parameter Problem type.
ICMPv4TypeParameterProblem = 12
// ICMPv6TypeDestinationUnreachable is the ICMPv6 Destination Unreachable type.
ICMPv6TypeDestinationUnreachable = 1
// ICMPv6TypePacketTooBig is the ICMPv6 Packet Too Big type.
ICMPv6TypePacketTooBig = 2
// ICMPv6TypeTimeExceeded is the ICMPv6 Time Exceeded type.
ICMPv6TypeTimeExceeded = 3
// ICMPv6TypeParameterProblem is the ICMPv6 Parameter Problem type.
ICMPv6TypeParameterProblem = 4
// ICMPv6TypeEchoRequest is the ICMPv6 Echo Request type.
ICMPv6TypeEchoRequest = 128
// ICMPv6TypeEchoReply is the ICMPv6 Echo Reply type.
ICMPv6TypeEchoReply = 129
// ICMPv4DestinationUnreachableCodeNetwork reports an unreachable destination network.
ICMPv4DestinationUnreachableCodeNetwork = 0
// ICMPv4DestinationUnreachableCodeHost reports an unreachable destination host.
ICMPv4DestinationUnreachableCodeHost = 1
// ICMPv4DestinationUnreachableCodeProtocol reports an unsupported destination protocol.
ICMPv4DestinationUnreachableCodeProtocol = 2
// ICMPv4DestinationUnreachableCodePort reports an unreachable destination port.
ICMPv4DestinationUnreachableCodePort = 3
// ICMPv4DestinationUnreachableCodeFragmentationNeeded reports a packet that requires fragmentation with DF set.
ICMPv4DestinationUnreachableCodeFragmentationNeeded = 4
// ICMPv4DestinationUnreachableCodeSourceRouteFailed reports a failed source route.
ICMPv4DestinationUnreachableCodeSourceRouteFailed = 5
// ICMPv4DestinationUnreachableCodeNetworkUnknown reports an unknown destination network.
ICMPv4DestinationUnreachableCodeNetworkUnknown = 6
// ICMPv4DestinationUnreachableCodeHostUnknown reports an unknown destination host.
ICMPv4DestinationUnreachableCodeHostUnknown = 7
// ICMPv4DestinationUnreachableCodeSourceHostIsolated reports an isolated source host.
ICMPv4DestinationUnreachableCodeSourceHostIsolated = 8
// ICMPv4DestinationUnreachableCodeNetworkAdministrativelyProhibited reports a prohibited destination network.
ICMPv4DestinationUnreachableCodeNetworkAdministrativelyProhibited = 9
// ICMPv4DestinationUnreachableCodeHostAdministrativelyProhibited reports a prohibited destination host.
ICMPv4DestinationUnreachableCodeHostAdministrativelyProhibited = 10
// ICMPv4DestinationUnreachableCodeNetworkUnreachableForTOS reports a network unreachable for the requested TOS.
ICMPv4DestinationUnreachableCodeNetworkUnreachableForTOS = 11
// ICMPv4DestinationUnreachableCodeHostUnreachableForTOS reports a host unreachable for the requested TOS.
ICMPv4DestinationUnreachableCodeHostUnreachableForTOS = 12
// ICMPv4DestinationUnreachableCodeCommunicationAdministrativelyProhibited reports prohibited communication.
ICMPv4DestinationUnreachableCodeCommunicationAdministrativelyProhibited = 13
// ICMPv4DestinationUnreachableCodeHostPrecedenceViolation reports a host-precedence violation.
ICMPv4DestinationUnreachableCodeHostPrecedenceViolation = 14
// ICMPv4DestinationUnreachableCodePrecedenceCutoff reports a precedence cutoff.
ICMPv4DestinationUnreachableCodePrecedenceCutoff = 15
// ICMPv4TimeExceededCodeTTLInTransit reports an expired IPv4 TTL in transit.
ICMPv4TimeExceededCodeTTLInTransit = 0
// ICMPv4TimeExceededCodeFragmentReassembly reports an expired fragment reassembly.
ICMPv4TimeExceededCodeFragmentReassembly = 1
// ICMPv4ParameterProblemCodePointer reports an error at the supplied pointer.
ICMPv4ParameterProblemCodePointer = 0
// ICMPv4ParameterProblemCodeMissingOption reports a missing required option.
ICMPv4ParameterProblemCodeMissingOption = 1
// ICMPv4ParameterProblemCodeBadLength reports an invalid packet length.
ICMPv4ParameterProblemCodeBadLength = 2
// ICMPv6DestinationUnreachableCodeNoRoute reports that no route exists.
ICMPv6DestinationUnreachableCodeNoRoute = 0
// ICMPv6DestinationUnreachableCodeAdministrativelyProhibited reports prohibited communication.
ICMPv6DestinationUnreachableCodeAdministrativelyProhibited = 1
// ICMPv6DestinationUnreachableCodeBeyondSourceScope reports a destination beyond the source address scope.
ICMPv6DestinationUnreachableCodeBeyondSourceScope = 2
// ICMPv6DestinationUnreachableCodeAddress reports an unreachable destination address.
ICMPv6DestinationUnreachableCodeAddress = 3
// ICMPv6DestinationUnreachableCodePort reports an unreachable destination port.
ICMPv6DestinationUnreachableCodePort = 4
// ICMPv6DestinationUnreachableCodeSourceAddressPolicy reports a source-address policy failure.
ICMPv6DestinationUnreachableCodeSourceAddressPolicy = 5
// ICMPv6DestinationUnreachableCodeRejectRoute reports a rejected destination route.
ICMPv6DestinationUnreachableCodeRejectRoute = 6
// ICMPv6DestinationUnreachableCodeSourceRoutingHeader reports an error in a Source Routing Header.
ICMPv6DestinationUnreachableCodeSourceRoutingHeader = 7
// ICMPv6DestinationUnreachableCodeHeadersTooLong reports that processing could not continue because the IPv6 headers were too long.
ICMPv6DestinationUnreachableCodeHeadersTooLong = 8
// ICMPv6DestinationUnreachableCodePRoute reports an error in an RPL P-Route.
ICMPv6DestinationUnreachableCodePRoute = 9
// ICMPv6TimeExceededCodeHopLimitInTransit reports an expired IPv6 Hop Limit in transit.
ICMPv6TimeExceededCodeHopLimitInTransit = 0
// ICMPv6TimeExceededCodeFragmentReassembly reports an expired fragment reassembly.
ICMPv6TimeExceededCodeFragmentReassembly = 1
// ICMPv6ParameterProblemCodeErroneousHeaderField reports an erroneous header field.
ICMPv6ParameterProblemCodeErroneousHeaderField = 0
// ICMPv6ParameterProblemCodeUnrecognizedNextHeader reports an unrecognized Next Header value.
ICMPv6ParameterProblemCodeUnrecognizedNextHeader = 1
// ICMPv6ParameterProblemCodeUnrecognizedOption reports an unrecognized IPv6 option.
ICMPv6ParameterProblemCodeUnrecognizedOption = 2
// ICMPv6ParameterProblemCodeIncompleteFirstFragment reports an incomplete first-fragment header chain.
ICMPv6ParameterProblemCodeIncompleteFirstFragment = 3
// ICMPv6ParameterProblemCodeSRUpperLayerHeader reports an SR upper-layer header error.
ICMPv6ParameterProblemCodeSRUpperLayerHeader = 4
// ICMPv6ParameterProblemCodeUnrecognizedNextHeaderAtIntermediateNode reports an unrecognized Next Header at an intermediate node.
ICMPv6ParameterProblemCodeUnrecognizedNextHeaderAtIntermediateNode = 5
// ICMPv6ParameterProblemCodeExtensionHeaderTooBig reports an extension header that exceeds a processing limit.
ICMPv6ParameterProblemCodeExtensionHeaderTooBig = 6
// ICMPv6ParameterProblemCodeExtensionHeaderChainTooLong reports an extension-header chain that exceeds a size limit.
ICMPv6ParameterProblemCodeExtensionHeaderChainTooLong = 7
// ICMPv6ParameterProblemCodeTooManyExtensionHeaders reports an extension-header count that exceeds a processing limit.
ICMPv6ParameterProblemCodeTooManyExtensionHeaders = 8
// ICMPv6ParameterProblemCodeTooManyOptionsInExtensionHeader reports an option count that exceeds a processing limit.
ICMPv6ParameterProblemCodeTooManyOptionsInExtensionHeader = 9
// ICMPv6ParameterProblemCodeOptionTooBig reports an option that exceeds a processing limit.
ICMPv6ParameterProblemCodeOptionTooBig = 10
// ICMPExtensionClassExtendedInformation identifies RFC 8883 Extended Information objects.
ICMPExtensionClassExtendedInformation = 4
// ICMPExtensionExtendedInformationTypePointer identifies an RFC 8883 Pointer object.
ICMPExtensionExtendedInformationTypePointer = 1
)
// ICMPMessage is the semantic representation of one ICMPv4 or ICMPv6
// message. Source and Destination select the address family and, for ICMPv6,
// provide the checksum pseudo-header. IPPacket.ICMPMessage borrows Body from
// the packet; callers must replace or copy it before modifying unowned input.
// Validation and wire encoding treat IPv4-mapped IPv6 addresses as IPv4
// without changing the receiver's address fields.
type ICMPMessage struct {
// Source is the source IP address.
Source netip.Addr
// Destination is the destination IP address.
Destination netip.Addr
// Type is the ICMP message type.
Type uint8
// Code is the subtype within Type.
Code uint8
// Body contains every byte after Type, Code, and Checksum. It must include
// the message type's four-byte minimum body.
Body []byte
}
// ICMPMessage validates and decodes the packet's family-appropriate ICMP
// upper layer.
func (p IPPacket) ICMPMessage() (ICMPMessage, error) {
protocol := byte(ProtocolICMPv4)
if p.Source.Unmap().Is6() {
protocol = ProtocolICMPv6
}
icmp, pseudoHeaderSafe, err := p.upperLayerForProtocol(protocol)
if err != nil {
return ICMPMessage{}, err
}
if protocol == ProtocolICMPv6 && !pseudoHeaderSafe {
return ICMPMessage{}, syscall.EPROTONOSUPPORT
}
if len(icmp) < 8 {
return ICMPMessage{}, syscall.EINVAL
}
valid := checksum(icmp) == 0
if protocol == ProtocolICMPv6 {
valid = transportChecksum(p.Source, p.Destination, ProtocolICMPv6, icmp) == 0
}
if !valid {
return ICMPMessage{}, syscall.EINVAL
}
return ICMPMessage{Source: p.Source.Unmap(), Destination: p.Destination.Unmap(), Type: icmp[0], Code: icmp[1], Body: icmp[4:]}, nil
}
// IsEchoRequest reports whether m is a complete family-appropriate Echo
// Request. The four leading Body bytes hold the echo identifier and sequence.
func (m ICMPMessage) IsEchoRequest() bool {
request, ok := m.echoKind()
return ok && request
}
// IsEchoReply reports whether m is a complete family-appropriate Echo Reply.
// The four leading Body bytes hold the echo identifier and sequence.
func (m ICMPMessage) IsEchoReply() bool {
request, ok := m.echoKind()
return ok && !request
}
// Echo returns the identifier, sequence, and data of a complete family-
// appropriate Echo Request or Echo Reply. Payload aliases Body.
func (m ICMPMessage) Echo() (identifier, sequence uint16, payload []byte, ok bool) {
if _, ok = m.echoKind(); !ok {
return 0, 0, nil, false
}
return binary.BigEndian.Uint16(m.Body[:2]), binary.BigEndian.Uint16(m.Body[2:4]), m.Body[4:], true
}
// classifyICMPEcho is the family-aware Echo grammar shared by public semantic
// values, raw Stack input, and Forwarder snapshots.
func classifyICMPEcho(protocol, messageType, code byte, bodyLength int) (request, ok bool) {
if bodyLength < 4 || code != ICMPCodeNone {
return false, false
}
switch protocol {
case ProtocolICMPv4:
switch messageType {
case ICMPv4TypeEchoRequest:
return true, true
case ICMPv4TypeEchoReply:
return false, true
}
case ProtocolICMPv6:
switch messageType {
case ICMPv6TypeEchoRequest:
return true, true
case ICMPv6TypeEchoReply:
return false, true
}
}
return false, false
}
// icmpEchoMessageType returns the request or reply type for one address family.
func icmpEchoMessageType(ipv6, request bool) byte {
if ipv6 {
if request {
return ICMPv6TypeEchoRequest
}
return ICMPv6TypeEchoReply
}
if request {
return ICMPv4TypeEchoRequest
}
return ICMPv4TypeEchoReply
}
// SetEchoRequest replaces Type, Code, and Body with an Echo Request containing
// identifier, sequence, and a copy of payload. Source and Destination must
// already select one valid address family. It leaves m unchanged on failure.
func (m *ICMPMessage) SetEchoRequest(identifier, sequence uint16, payload []byte) error {
return m.setEcho(true, identifier, sequence, payload)
}
// SetEchoReply replaces Type, Code, and Body with an Echo Reply containing
// identifier, sequence, and a copy of payload. Source and Destination must
// already select one valid address family. It leaves m unchanged on failure.
func (m *ICMPMessage) SetEchoReply(identifier, sequence uint16, payload []byte) error {
return m.setEcho(false, identifier, sequence, payload)
}
// echoKind validates the common Echo envelope and reports whether it is a
// request. A false request with ok set is an Echo Reply.
func (m ICMPMessage) echoKind() (request, ok bool) {
_, _, protocol, valid := normalizeICMPAddresses(m.Source, m.Destination)
if !valid {
return false, false
}
return classifyICMPEcho(protocol, m.Type, m.Code, len(m.Body))
}
// setEcho implements the two ownership-identical Echo construction methods.
func (m *ICMPMessage) setEcho(request bool, identifier, sequence uint16, payload []byte) error {
if m == nil {
return syscall.EINVAL
}
_, _, protocol, valid := normalizeICMPAddresses(m.Source, m.Destination)
if !valid {
return syscall.EINVAL
}
if len(payload) > 65535-8 {
return syscall.EMSGSIZE
}
body := make([]byte, 4+len(payload))
binary.BigEndian.PutUint16(body[:2], identifier)
binary.BigEndian.PutUint16(body[2:4], sequence)
copy(body[4:], payload)
messageType := icmpEchoMessageType(protocol == ProtocolICMPv6, request)
m.Type, m.Code, m.Body = messageType, ICMPCodeNone, body
return nil
}
// EchoReply returns the semantic Echo Reply corresponding to m, using source
// as the reply source address. An explicit source is required because a request
// destination may be multicast, broadcast, or anycast and source selection
// requires IP routing state. The source argument must be an unzoned address in
// m's address family, but its ownership and address classification are not
// checked. Body aliases m.Body; MarshalBinary or AppendBinary calculates the
// reply checksum.
func (m ICMPMessage) EchoReply(source netip.Addr) (ICMPMessage, error) {
normalized, _, err := m.wireLayout()
if err != nil {
return ICMPMessage{}, err
}
if !normalized.IsEchoRequest() || source.Zone() != "" {
return ICMPMessage{}, syscall.EINVAL
}
source = source.Unmap()
if !source.IsValid() || source.Is4() != normalized.Source.Is4() {
return ICMPMessage{}, syscall.EINVAL
}
normalized.Destination = normalized.Source
normalized.Source = source
normalized.Type = icmpEchoMessageType(source.Is6(), false)
return normalized, nil
}
// IsError reports whether m has a family-appropriate ICMP error type and code
// accepted on receive, including unknown ICMPv6 error types. It classifies the
// message without parsing its quoted packet; ICMPError validates the quote.
func (m ICMPMessage) IsError() bool {
_, _, protocol, valid := normalizeICMPAddresses(m.Source, m.Destination)
if !valid {
return false
}
return isICMPError(protocol, m.Type, m.Code)
}
// ICMPError validates and decodes m as a received ICMP error. QuotedPacket,
// QuotedPayload, and Extensions borrow m.Body, and available TCP or UDP ports
// are populated immediately. The parser accepts the intentionally truncated
// quotations that RFC 792 and RFC 4443 permit and does not apply socket-
// correlation policy.
func (m ICMPMessage) ICMPError() (ICMPError, error) {
normalized, _, err := m.wireLayout()
if err != nil {
return ICMPError{}, err
}
protocol := byte(ProtocolICMPv4)
if normalized.Source.Is6() {
protocol = ProtocolICMPv6
}
result, valid := parseICMPErrorFields(normalized.Source, protocol, normalized.Type, normalized.Code, normalized.Body)
if !valid {
return ICMPError{}, syscall.EINVAL
}
return result, nil
}
// MarshalBinary returns the complete ICMP message wire encoding. Source and
// Destination select the family and contribute to the ICMPv6 pseudo-header
// checksum but are not themselves encoded. MarshalBinary is semantically
// identical to AppendBinary(nil).
func (m ICMPMessage) MarshalBinary() ([]byte, error) { return m.AppendBinary(nil) }
// AppendBinary appends the complete ICMP message wire encoding to dst. Source
// and Destination select the family and contribute to the ICMPv6 pseudo-header
// checksum but are not themselves encoded. It validates every field before
// changing dst, does not retain any input slice, and permits the destination
// to share backing storage with Body. On validation failure it returns the
// original dst unchanged.
func (m ICMPMessage) AppendBinary(dst []byte) ([]byte, error) {
normalized, totalSize, err := m.wireLayout()
if err != nil {
return dst, err
}
start := len(dst)
dst = extendForAppend(dst, totalSize)
marshalPublicICMPMessage(dst[start:], normalized)
return dst, nil
}
// wireLayout validates m, normalizes its addresses, and returns its exact
// message length.
func (m ICMPMessage) wireLayout() (ICMPMessage, int, error) {
source, destination, _, valid := normalizeICMPAddresses(m.Source, m.Destination)
if !valid {
return ICMPMessage{}, 0, syscall.EINVAL
}
m.Source, m.Destination = source, destination
if len(m.Body) < 4 {
return ICMPMessage{}, 0, syscall.EINVAL
}
if len(m.Body) > 65535-4 {
return ICMPMessage{}, 0, syscall.EMSGSIZE
}
return m, 4 + len(m.Body), nil
}
// marshalPublicICMPMessage writes one already validated semantic message.
func marshalPublicICMPMessage(dst []byte, m ICMPMessage) {
copy(dst[4:], m.Body)
dst[0], dst[1], dst[2], dst[3] = m.Type, m.Code, 0, 0
value := checksum(dst)
if m.Source.Is6() {
value = transportChecksum(m.Source, m.Destination, ProtocolICMPv6, dst)
}
binary.BigEndian.PutUint16(dst[2:4], value)
}
// ICMPExtensionObject is one RFC 4884 extension object in semantic wire order.
// Data excludes the four-byte object header. ExtensionObjects returns Data
// slices that borrow ICMPError.Extensions, while SetExtensionObjects copies
// every Data slice. Unknown classes and types, duplicates, and order are
// preserved.
type ICMPExtensionObject struct {
// Class is the object's Class-Num.
Class uint8
// Type is the class-specific C-Type.
Type uint8
// Data is the object payload following Length, Class-Num, and C-Type.
Data []byte
}
// Pointer returns the byte offset carried by an RFC 8883 Extended Information
// Pointer object.
func (o ICMPExtensionObject) Pointer() (uint32, bool) {
if o.Class != ICMPExtensionClassExtendedInformation ||
o.Type != ICMPExtensionExtendedInformationTypePointer || len(o.Data) != 4 {
return 0, false
}
return binary.BigEndian.Uint32(o.Data), true
}
// SetPointer replaces o with an RFC 8883 Extended Information Pointer object.
func (o *ICMPExtensionObject) SetPointer(pointer uint32) {
o.Class = ICMPExtensionClassExtendedInformation
o.Type = ICMPExtensionExtendedInformationTypePointer
o.Data = make([]byte, 4)
binary.BigEndian.PutUint32(o.Data, pointer)
}
// ICMPError describes a validated remote network error.
type ICMPError struct {
// Reporter is the router or destination that generated the error.
Reporter netip.Addr
// Type is the wire ICMP error type.
Type byte
// Code retains the wire subtype within Type.
Code byte
// MTU is present for fragmentation-needed and packet-too-big errors. It is
// also consumed by ICMPError.ICMPMessage when the error type defines that
// field.
MTU uint32
// Pointer is present for IPv4 or IPv6 Parameter Problem errors. It is also
// consumed by ICMPError.ICMPMessage when the error type defines that field.
Pointer uint32
// Extensions contains the encoded RFC 4884 extension-object sequence,
// excluding the four-byte Extension Header. Parsed errors borrow the ICMP
// message body. ICMPError.ICMPMessage validates and copies this storage.
Extensions []byte
// QuotedSource is the source of the failed original packet. It is derived
// from QuotedPacket and ignored by ICMPError.ICMPMessage.
QuotedSource netip.Addr
// QuotedTarget is the destination of the failed original packet. It is
// derived from QuotedPacket and ignored by ICMPError.ICMPMessage.
QuotedTarget netip.Addr
// QuotedProtocol is the final protocol identified in the available quote.
// It is derived from QuotedPacket and ignored by ICMPError.ICMPMessage.
QuotedProtocol byte
// QuotedPacket contains the available original IP packet, including its IP
// header. ICMPError.ICMPMessage copies it into the constructed error.
// QuotedPayload aliases its upper-layer suffix when both are present.
QuotedPacket []byte
// QuotedPayload contains the available original transport header bytes. It
// is derived from QuotedPacket and ignored by ICMPError.ICMPMessage.
QuotedPayload []byte
// QuotedSourcePort is the original TCP or UDP source port when present. It is
// derived from QuotedPacket and ignored by ICMPError.ICMPMessage.
QuotedSourcePort uint16
// QuotedTargetPort is the original TCP or UDP destination port when present.
// It is derived from QuotedPacket and ignored by ICMPError.ICMPMessage.
QuotedTargetPort uint16
}
const (
icmpExtensionVersion = 2
icmpExtensionHeaderSize = 4
icmpExtensionObjectHeaderSize = 4
icmpExtensionMinimumQuoteSize = 128
maximumICMPExtensionDataSize = 65535 - 8 - icmpExtensionMinimumQuoteSize - icmpExtensionHeaderSize
)
// icmpExtensionObjectCursor shares object framing between the public parser
// and validation on the stack receive path without allocating descriptors.
type icmpExtensionObjectCursor struct {
remaining []byte
}
// next returns one borrowed object. valid distinguishes the end of a valid
// sequence from malformed framing.
func (c *icmpExtensionObjectCursor) next() (object ICMPExtensionObject, ok, valid bool) {
if len(c.remaining) == 0 {
return ICMPExtensionObject{}, false, true
}
if len(c.remaining) < icmpExtensionObjectHeaderSize {
return ICMPExtensionObject{}, false, false
}
length := int(binary.BigEndian.Uint16(c.remaining[:2]))
if length < icmpExtensionObjectHeaderSize || length&3 != 0 || length > len(c.remaining) {
return ICMPExtensionObject{}, false, false
}
object = ICMPExtensionObject{
Class: c.remaining[2], Type: c.remaining[3],
Data: c.remaining[icmpExtensionObjectHeaderSize:length],
}
c.remaining = c.remaining[length:]
return object, true, true
}
// validateICMPExtensionObjects validates one nonempty object sequence and
// reports whether it contains an RFC 8883 Pointer object.
func validateICMPExtensionObjects(encoded []byte) (hasPointer, valid bool) {
cursor := icmpExtensionObjectCursor{remaining: encoded}
count := 0
for {
object, ok, framingValid := cursor.next()
if !framingValid {
return false, false
}
if !ok {
return hasPointer, count != 0
}
count++
if _, pointer := object.Pointer(); pointer {
hasPointer = true
}
}
}
// ExtensionObjects parses the RFC 4884 object sequence. The returned slice
// owns its descriptors, but every Data field borrows e.Extensions. An error
// reports malformed object framing. An ICMPError with no extensions returns a
// nil slice and nil error.
func (e ICMPError) ExtensionObjects() ([]ICMPExtensionObject, error) {
if len(e.Extensions) == 0 {
return nil, nil
}
cursor := icmpExtensionObjectCursor{remaining: e.Extensions}
objects := make([]ICMPExtensionObject, 0, 1)
for {
object, ok, valid := cursor.next()
if !valid {
return nil, syscall.EINVAL
}
if !ok {
return objects, nil
}
objects = append(objects, object)
}
}
// SetExtensionObjects replaces Extensions with an encoded RFC 4884 object
// sequence. Each Data length must be a multiple of four bytes. The method
// preserves unknown classes and types, duplicates, and order, copies every
// input Data slice, and leaves e unchanged on failure. An empty sequence clears
// Extensions; an encoded Extension Structure is generated only when at least
// one object is present.
func (e *ICMPError) SetExtensionObjects(objects []ICMPExtensionObject) error {
if e == nil {
return syscall.EINVAL
}
if len(objects) == 0 {
e.Extensions = nil
return nil
}
size := 0
for _, object := range objects {
if len(object.Data) > 1<<16-1-icmpExtensionObjectHeaderSize {
return syscall.EMSGSIZE
}
if len(object.Data)&3 != 0 {
return syscall.EINVAL
}
length := icmpExtensionObjectHeaderSize + len(object.Data)
if size > maximumICMPExtensionDataSize-length {
return syscall.EMSGSIZE
}
size += length
}
encoded := make([]byte, size)
offset := 0
for _, object := range objects {
length := icmpExtensionObjectHeaderSize + len(object.Data)
binary.BigEndian.PutUint16(encoded[offset:offset+2], uint16(length))
encoded[offset+2], encoded[offset+3] = object.Class, object.Type
copy(encoded[offset+icmpExtensionObjectHeaderSize:offset+length], object.Data)
offset += length
}
e.Extensions = encoded
return nil
}
// icmpErrorFieldKinds identifies the four-byte error-body fields whose wire
// layout is shared by public construction and Stack-generated control errors.
func icmpErrorFieldKinds(protocol, messageType, code byte) (mtu, pointer bool) {
mtu = protocol == ProtocolICMPv4 && messageType == ICMPv4TypeDestinationUnreachable && code == ICMPv4DestinationUnreachableCodeFragmentationNeeded ||
protocol == ProtocolICMPv6 && messageType == ICMPv6TypePacketTooBig
pointer = protocol == ProtocolICMPv4 && messageType == ICMPv4TypeParameterProblem ||
protocol == ProtocolICMPv6 && messageType == ICMPv6TypeParameterProblem
return
}
// marshalICMPErrorBody writes the four type-specific bytes and quoted packet
// after validation by the public constructor or selection by Stack policy.
func marshalICMPErrorBody(body []byte, protocol, messageType, code byte, mtu, pointer uint32, quote []byte) {
body[0], body[1], body[2], body[3] = 0, 0, 0, 0
mtuField, pointerField := icmpErrorFieldKinds(protocol, messageType, code)
if mtuField {
if protocol == ProtocolICMPv4 {
binary.BigEndian.PutUint16(body[2:4], uint16(mtu))
} else {
binary.BigEndian.PutUint32(body[:4], mtu)
}
} else if pointerField {
if protocol == ProtocolICMPv4 {
body[0] = byte(pointer)
} else {
binary.BigEndian.PutUint32(body[:4], pointer)
}
}
copy(body[4:], quote)
}
// icmpExtensionLayout returns the RFC 4884 Length-field offset within the
// four-byte error body and its address-family unit.
func icmpExtensionLayout(protocol, messageType byte) (lengthOffset, unit int, ok bool) {
if protocol == ProtocolICMPv4 {
switch messageType {
case ICMPv4TypeDestinationUnreachable, ICMPv4TypeTimeExceeded, ICMPv4TypeParameterProblem:
return 1, 4, true
}
}
if protocol == ProtocolICMPv6 {
switch messageType {
case ICMPv6TypeDestinationUnreachable, ICMPv6TypeTimeExceeded:
return 0, 8, true
}
}
return 0, 0, false
}
// parseICMPExtensionStructure validates the RFC 4884 header, optional
// checksum, and complete nonempty object sequence. The returned bytes exclude
// the Extension Header and borrow structure.
func parseICMPExtensionStructure(structure []byte) (objects []byte, hasPointer, ok bool) {
if len(structure) < icmpExtensionHeaderSize+icmpExtensionObjectHeaderSize ||
structure[0]>>4 != icmpExtensionVersion {
return nil, false, false
}
if binary.BigEndian.Uint16(structure[2:4]) != 0 && checksum(structure) != 0 {
return nil, false, false
}
objects = structure[icmpExtensionHeaderSize:]
hasPointer, valid := validateICMPExtensionObjects(objects)
if !valid {
return nil, false, false
}
return objects, hasPointer, true
}
// quotedIPDeclaredLength returns the original IP length when its base header
// makes that boundary unambiguous. IPv6 Payload Length zero remains unknown
// because the quote may be a jumbogram.
func quotedIPDeclaredLength(quote []byte) (int, bool) {
if len(quote) == 0 {
return 0, false
}
switch quote[0] >> 4 {
case 4:
if len(quote) < 20 {
return 0, false
}
headerSize := int(quote[0]&0x0f) * 4
totalSize := int(binary.BigEndian.Uint16(quote[2:4]))
if headerSize < 20 || totalSize < headerSize {
return 0, false
}
return totalSize, true
case 6:
if len(quote) < 40 {
return 0, false
}
payloadSize := int(binary.BigEndian.Uint16(quote[4:6]))
if payloadSize == 0 {
// A zero Payload Length with a Hop-by-Hop header may identify a
// jumbogram. Every other Next Header unambiguously describes an
// ordinary zero-payload packet.
if quote[6] == IPv6ExtensionHeaderHopByHop {
return 0, false
}
return 40, true
}
return 40 + payloadSize, true
default:
return 0, false
}
}
// trimICMPExtensionQuote removes RFC 4884 padding only when the quoted IP
// header proves the original boundary. Nonzero bytes beyond a proven boundary
// violate the required zero padding.
func trimICMPExtensionQuote(quote []byte) ([]byte, bool) {
declared, known := quotedIPDeclaredLength(quote)
if !known || declared > len(quote) {
return quote, true
}
for _, value := range quote[declared:] {
if value != 0 {
return nil, false
}
}
return quote[:declared], true
}
// splitICMPErrorQuote separates a compliant RFC 4884 Extension Structure from
// its padded quote. A zero Length field always means that no extensions are
// present, as required for compatibility by RFC 4884 section 5.4.
func splitICMPErrorQuote(protocol, messageType byte, body []byte) (quote, extensions []byte, hasPointer, ok bool) {
lengthOffset, unit, extensible := icmpExtensionLayout(protocol, messageType)
if !extensible || body[lengthOffset] == 0 {
return body[4:], nil, false, true
}
paddedLength := int(body[lengthOffset]) * unit
if paddedLength < icmpExtensionMinimumQuoteSize || len(body) < 4+paddedLength+icmpExtensionHeaderSize+icmpExtensionObjectHeaderSize {
return nil, nil, false, false
}
extensions, hasPointer, valid := parseICMPExtensionStructure(body[4+paddedLength:])
if !valid {
return nil, nil, false, false
}
quote, valid = trimICMPExtensionQuote(body[4 : 4+paddedLength])
if !valid {
return nil, nil, false, false
}
return quote, extensions, hasPointer, true
}
// icmpExtensionQuoteLength validates that zero padding cannot replace known
// quoted bytes, then returns the representable padded quote length.
func icmpExtensionQuoteLength(protocol, messageType byte, quote []byte) (int, error) {
_, unit, ok := icmpExtensionLayout(protocol, messageType)
if !ok {
return 0, syscall.EINVAL
}
if declared, known := quotedIPDeclaredLength(quote); known {
if declared <= len(quote) {
for _, value := range quote[declared:] {
if value != 0 {
return 0, syscall.EINVAL
}
}
} else if len(quote) < icmpExtensionMinimumQuoteSize {
return 0, syscall.EINVAL
}
} else if len(quote) < icmpExtensionMinimumQuoteSize {
return 0, syscall.EINVAL
}
paddedLength := len(quote)
if paddedLength < icmpExtensionMinimumQuoteSize {
paddedLength = icmpExtensionMinimumQuoteSize
}
paddedLength = (paddedLength + unit - 1) &^ (unit - 1)
if paddedLength/unit > 255 {
return 0, syscall.EMSGSIZE
}
return paddedLength, nil
}
// marshalICMPExtensionStructure writes one validated RFC 4884 structure.
func marshalICMPExtensionStructure(structure, objects []byte) {
structure[0], structure[1], structure[2], structure[3] = icmpExtensionVersion<<4, 0, 0, 0
copy(structure[icmpExtensionHeaderSize:], objects)
binary.BigEndian.PutUint16(structure[2:4], checksum(structure))
}
// ICMPMessage constructs the semantic ICMP error represented by e for
// destination. Reporter and destination select the address family; Type, Code,
// MTU, Pointer, QuotedPacket, and Extensions provide the wire fields. It
// validates the possibly truncated quote and extension objects, copies both
// byte slices, and ignores the fields derived from the quote. Routing, source
// ownership, rate limits, quote truncation, and recursive-error suppression
// remain Stack transmission policy.
//
// Unlike receive classification, construction requires a supported type/code
// pair, including Code 0 for ICMPv6 Packet Too Big.
func (e ICMPError) ICMPMessage(destination netip.Addr) (ICMPMessage, error) {
reporter, destination, protocol, valid := normalizeICMPAddresses(e.Reporter, destination)
if !valid || !validICMPErrorCode(protocol, e.Type, e.Code) {
return ICMPMessage{}, syscall.EINVAL
}
if len(e.QuotedPacket) > 65535-8 {
return ICMPMessage{}, syscall.EMSGSIZE
}
quotedSource, quotedTarget, _, _, quoted := quotedIPPayload(e.QuotedPacket)
if !quoted || protocol == ProtocolICMPv4 && (!quotedSource.Is4() || !quotedTarget.Is4()) ||
protocol == ProtocolICMPv6 && (!quotedSource.Is6() || !quotedTarget.Is6()) {
return ICMPMessage{}, syscall.EINVAL
}
mtuField, pointerField := icmpErrorFieldKinds(protocol, e.Type, e.Code)
if !mtuField && e.MTU != 0 || !pointerField && e.Pointer != 0 ||
protocol == ProtocolICMPv4 && mtuField && e.MTU > 1<<16-1 ||
protocol == ProtocolICMPv4 && pointerField && e.Pointer > 1<<8-1 {
return ICMPMessage{}, syscall.EINVAL
}
if len(e.Extensions) == 0 {
if protocol == ProtocolICMPv6 && e.Type == ICMPv6TypeDestinationUnreachable &&
e.Code == ICMPv6DestinationUnreachableCodeHeadersTooLong {
return ICMPMessage{}, syscall.EINVAL
}
body := make([]byte, 4+len(e.QuotedPacket))
marshalICMPErrorBody(body, protocol, e.Type, e.Code, e.MTU, e.Pointer, e.QuotedPacket)
return ICMPMessage{Source: reporter, Destination: destination, Type: e.Type, Code: e.Code, Body: body}, nil
}
if len(e.Extensions) > maximumICMPExtensionDataSize {
return ICMPMessage{}, syscall.EMSGSIZE
}
hasPointer, validExtensions := validateICMPExtensionObjects(e.Extensions)
if !validExtensions || protocol == ProtocolICMPv6 && e.Type == ICMPv6TypeDestinationUnreachable &&
e.Code == ICMPv6DestinationUnreachableCodeHeadersTooLong && !hasPointer {
return ICMPMessage{}, syscall.EINVAL
}
paddedQuoteLength, err := icmpExtensionQuoteLength(protocol, e.Type, e.QuotedPacket)
if err != nil {
return ICMPMessage{}, err
}
bodyLength := 4 + paddedQuoteLength + icmpExtensionHeaderSize + len(e.Extensions)
if bodyLength > 65535-4 {
return ICMPMessage{}, syscall.EMSGSIZE
}
body := make([]byte, bodyLength)
marshalICMPErrorBody(body, protocol, e.Type, e.Code, e.MTU, e.Pointer, e.QuotedPacket)
lengthOffset, unit, _ := icmpExtensionLayout(protocol, e.Type)
body[lengthOffset] = byte(paddedQuoteLength / unit)
marshalICMPExtensionStructure(body[4+paddedQuoteLength:], e.Extensions)
return ICMPMessage{Source: reporter, Destination: destination, Type: e.Type, Code: e.Code, Body: body}, nil
}
// normalizeICMPAddresses validates and normalizes the address-family context
// shared by the public ICMP codecs.
func normalizeICMPAddresses(source, destination netip.Addr) (netip.Addr, netip.Addr, byte, bool) {
if source.Zone() != "" || destination.Zone() != "" {
return netip.Addr{}, netip.Addr{}, 0, false
}
source, destination = source.Unmap(), destination.Unmap()
if !source.IsValid() || !destination.IsValid() || source.Is4() != destination.Is4() {
return netip.Addr{}, netip.Addr{}, 0, false
}
if source.Is4() {
return source, destination, ProtocolICMPv4, true
}
return source, destination, ProtocolICMPv6, true
}
// icmpForwarderIPPacket is one caller-owned, normalized header-included ICMP
// reply. parsed and ipv6Fragment describe slices within packet.
type icmpForwarderIPPacket struct {
packet []byte
parsed ipPacket
ipv4DF bool
ipv6Fragment ipv6FragmentPoint
}
// validateICMPForwarderReplyPayload applies the ICMP message and address-family
// size limits. RFC 4443 requires an ICMPv6 error to fit the 1280-byte minimum
// IPv6 MTU even when informational messages may use source fragmentation.
func validateICMPForwarderReplyPayload(ipv6 bool, payload []byte) error {
if len(payload) < 8 {
return syscall.EINVAL
}
maximum := 65535
if !ipv6 {
maximum -= 20
}
if len(payload) > maximum {
return syscall.EMSGSIZE
}
if ipv6 && payload[0] < 128 && len(payload) > ipv6MinimumMTU-40 {
return syscall.EMSGSIZE
}
return nil
}
// prepareICMPForwarderIPPacket validates and normalizes a restricted raw ICMP
// reply without consulting mutable stack state. The input is copied before any
// field is changed.
func prepareICMPForwarderIPPacket(input []byte, destination netip.Addr) (icmpForwarderIPPacket, error) {
destination = destination.Unmap()
if !destination.IsValid() || len(input) == 0 {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
switch input[0] >> 4 {
case 4:
if len(input) > 65535 {
return icmpForwarderIPPacket{}, syscall.EMSGSIZE
}
case 6:
if len(input)-40 > 65535 {
return icmpForwarderIPPacket{}, syscall.EMSGSIZE
}
default:
return icmpForwarderIPPacket{}, syscall.EINVAL
}
packet := append([]byte(nil), input...)
result := icmpForwarderIPPacket{packet: packet}
switch packet[0] >> 4 {
case 4:
if destination.Is6() || len(packet) < 28 || len(packet) > 65535 {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
headerSize := int(packet[0]&0x0f) * 4
field := binary.BigEndian.Uint16(packet[6:8])
if headerSize < 20 || headerSize > len(packet)-8 || field&0xbfff != 0 || packet[9] != ProtocolICMPv4 {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
contentSize, validOptions := ipv4OptionsContentLength(packet[20:headerSize])
if !validOptions {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
for index := 20 + contentSize; index < headerSize; index++ {
packet[index] = 0
}
binary.BigEndian.PutUint16(packet[2:4], uint16(len(packet)))
packet[10], packet[11] = 0, 0
binary.BigEndian.PutUint16(packet[10:12], checksum(packet[:headerSize]))
result.ipv4DF = field&0x4000 != 0
case 6:
if destination.Is4() || len(packet) < 48 || len(packet)-40 > 65535 {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
binary.BigEndian.PutUint16(packet[4:6], uint16(len(packet)-40))
point, ok := inspectIPv6ForwarderFragmentPoint(packet)
if !ok {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
result.ipv6Fragment = point
if point.atomicOffset >= 0 {
// Reserved bits are ignored by receivers but must be zero in output.
packet[point.atomicOffset+1] = 0
binary.BigEndian.PutUint16(packet[point.atomicOffset+2:point.atomicOffset+4], 0)
}
default:
return icmpForwarderIPPacket{}, syscall.EINVAL
}
parsed, ok := parseIPPacket(packet, false)
if !ok || parsed.parameterError || len(parsed.payload) < 8 || parsed.target != destination {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
if parsed.source.Is4() {
if parsed.protocol != ProtocolICMPv4 {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
parsed.payload[2], parsed.payload[3] = 0, 0
binary.BigEndian.PutUint16(parsed.payload[2:4], checksum(parsed.payload))
} else {
if parsed.protocol != ProtocolICMPv6 {
return icmpForwarderIPPacket{}, syscall.EINVAL
}
// RFC 4443 requires every ICMPv6 error to fit the 1280-byte minimum
// IPv6 MTU. Informational messages may still use source fragmentation.
if parsed.payload[0] < 128 && len(packet) > ipv6MinimumMTU {
return icmpForwarderIPPacket{}, syscall.EMSGSIZE
}
parsed.payload[2], parsed.payload[3] = 0, 0
binary.BigEndian.PutUint16(parsed.payload[2:4], transportChecksum(parsed.source, parsed.target, ProtocolICMPv6, parsed.payload))
}
result.parsed = parsed
return result, nil
}
// writeICMPForwarderIPPacket revalidates interception and return routing, then
// makes one best-effort output attempt for the normalized header-included packet
// or its source fragments.
func (s *Stack) writeICMPForwarderIPPacket(request ipPacket, reply icmpForwarderIPPacket) error {
state := s.network.Load()
if !state.acceptsInboundDestination(request.target) {
return syscall.EADDRNOTAVAIL
}
if _, routed := state.routeFor(reply.parsed.target); !routed {
return syscall.ENETUNREACH
}
packets, err := s.icmpForwarderIPPackets(reply, s.mtuFor(reply.parsed.target))
if err != nil {
return err
}
var flow outputFlowKey
if len(packets) > 1 {
flow = s.outbound.ipFlowKey(reply.parsed.source, reply.parsed.target, reply.parsed.protocol, reply.parsed.flowLabel, reply.parsed.payload)
}
err = s.tryWritePackets(packets, flow, true)
if err == ErrResourceLimit {
return nil
}
return err
}
// Error formats the remote ICMP failure.
func (e ICMPError) Error() string {
if e.MTU != 0 {
return fmt.Sprintf("ICMP error from %s: type=%d code=%d mtu=%d", e.Reporter, e.Type, e.Code, e.MTU)
}
return fmt.Sprintf("ICMP error from %s: type=%d code=%d", e.Reporter, e.Type, e.Code)