-
Notifications
You must be signed in to change notification settings - Fork 11
/
update.go
1174 lines (1094 loc) · 37.5 KB
/
update.go
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 corebgp
import (
"encoding/binary"
"errors"
"fmt"
"net/netip"
)
// PathAttrFlags represents the flags for a path attribute.
type PathAttrFlags uint8
// Optional defines whether the attribute is optional (if set to 1) or
// well-known (if set to 0).
func (p PathAttrFlags) Optional() bool {
return 1<<7&p != 0
}
// Transitive defines whether an optional attribute is transitive (if set to 1)
// or non-transitive (if set to 0).
func (p PathAttrFlags) Transitive() bool {
return 1<<6&p != 0
}
// Partial defines whether the information contained in the optional transitive
// attribute is partial (if set to 1) or complete (if set to 0).
func (p PathAttrFlags) Partial() bool {
return 1<<5&p != 0
}
// ExtendedLen defines whether the Attribute Length is one octet (if set to 0)
// or two octets (if set to 1).
func (p PathAttrFlags) ExtendedLen() bool {
return 1<<4&p != 0
}
func (p PathAttrFlags) Validate(forCode uint8, attrData []byte, wantOptional, wantTransitive bool) error {
// https://www.rfc-editor.org/rfc/rfc7606#page-6
// If the value of either the Optional or Transitive bits in the Attribute
// Flags is in conflict with their specified values, then the attribute MUST
// be treated as malformed and the "treat-as-withdraw" approach used, unless
// the specification for the attribute mandates different handling for
// incorrect Attribute Flags.
if p.Optional() != wantOptional || p.Transitive() != wantTransitive {
return &TreatAsWithdrawUpdateErr{
Code: forCode,
Notification: &Notification{ // fallback RFC4271 handling
// https://www.rfc-editor.org/rfc/rfc4271#page-32
// If any recognized attribute has Attribute Flags that conflict
// with the Attribute Type Code, then the Error Subcode MUST be
// set to Attribute Flags Error. The Data field MUST contain
// the erroneous attribute (type, length, and value).
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
Subcode: NOTIF_SUBCODE_ATTR_FLAGS_ERR,
Data: notifDataForAttrBasedErr(forCode, attrData),
},
}
}
return nil
}
type OriginPathAttr uint8
// UpdateNotificationFromErr finds the highest severity *Notification in err's
// tree. This is useful for Plugins using UpdateDecoder that do not handle the
// additional error approaches described by RFC7606, and instead are designed to
// send a *Notification when any error occurs.
//
// The severity order from highest to lowest is *Notification,
// *TreatAsWithdrawUpdateErr, *AttrDiscardUpdateErr, UpdateError (not one of the
// previous concrete types). If there are multiple errors contained in err's
// tree of the same severity the earliest found is returned. A *Notification
// with code NOTIF_CODE_UPDATE_MESSAGE_ERR, no subcode, and no data is returned
// in the event that a *Notification cannot be extracted from err.
func UpdateNotificationFromErr(err error) *Notification {
if err == nil {
return nil
}
var (
n *Notification
taw *TreatAsWithdrawUpdateErr
ad *AttrDiscardUpdateErr
ue UpdateError
)
var unwrap func(err error)
unwrap = func(err error) {
switch x := err.(type) {
case *Notification:
if n == nil {
n = x
}
return
case *TreatAsWithdrawUpdateErr:
if taw == nil {
taw = x
}
case *AttrDiscardUpdateErr:
if ad == nil {
ad = x
}
case UpdateError:
if ue == nil {
ue = x
}
}
switch x := err.(type) {
case interface{ Unwrap() error }:
unwrap(x.Unwrap())
if n != nil {
return
}
case interface{ Unwrap() []error }:
for _, err = range x.Unwrap() {
unwrap(err)
if n != nil {
return
}
}
}
}
unwrap(err)
if n != nil {
return n
} else if taw != nil {
return taw.AsSessionReset()
} else if ad != nil {
return ad.AsSessionReset()
} else if ue != nil {
return ue.AsSessionReset()
}
return &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
}
}
// UpdateError represents an error handling an UPDATE message. UpdateError is
// used throughout the decoding logic in this package. It provides the option of
// following either the UPDATE error handling originally defined in RFC4271 OR
// the revised handling defined in RFC7606.
type UpdateError interface {
error
// AsSessionReset returns a *Notification that can be used to represent
// the error as a NOTIFICATION message to be sent to the remote peer. This
// can be used where usage of UpdateDecoder does not implement the revised
// error handling described by RFC7606, and instead intends for all errors
// to result in a session reset.
AsSessionReset() *Notification
}
// AttrDiscardUpdateErr represents an error encountered during UPDATE message
// handling. The usage of this error is described in
// https://www.rfc-editor.org/rfc/rfc7606#section-2
//
// Attribute discard: In this approach, the malformed attribute MUST be
// discarded and the UPDATE message continues to be processed. This approach
// MUST NOT be used except in the case of an attribute that has no effect on
// route selection or installation.
type AttrDiscardUpdateErr struct {
Code uint8
Notification *Notification
}
func (a *AttrDiscardUpdateErr) AsSessionReset() *Notification {
if a.Notification != nil {
return a.Notification
}
return &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
}
}
func (a *AttrDiscardUpdateErr) Error() string {
return fmt.Sprintf("attribute discard error for attribute code: %d", a.Code)
}
// TreatAsWithdrawUpdateErr represents an error encountered during UPDATE
// message handling. The usage of this error is described in
// https://www.rfc-editor.org/rfc/rfc7606#section-2
//
// Treat-as-withdraw: In this approach, the UPDATE message containing the path
// attribute in question MUST be treated as though all contained routes had been
// withdrawn just as if they had been listed in the WITHDRAWN ROUTES field (or
// in the MP_UNREACH_NLRI attribute if appropriate) of the UPDATE message, thus
// causing them to be removed from the Adj-RIB-In according to the procedures of
// [RFC4271].
type TreatAsWithdrawUpdateErr struct {
Code uint8
Notification *Notification
}
func (t *TreatAsWithdrawUpdateErr) AsSessionReset() *Notification {
if t.Notification != nil {
return t.Notification
}
return &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
}
}
func (t *TreatAsWithdrawUpdateErr) Error() string {
return fmt.Sprintf("treat as withdraw error for attribute code: %d", t.Code)
}
func (o *OriginPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_ORIGIN, b, false, true)
if err != nil {
return err
}
if len(b) != 1 || b[0] > 2 {
// https://www.rfc-editor.org/rfc/rfc7606#page-11
// The attribute is considered malformed if its length is not 1 or if it
// has an undefined value [RFC4271].
//
// An UPDATE message with a malformed ORIGIN attribute SHALL be handled
// using the approach of "treat-as-withdraw".
var subcode uint8
if len(b) != 1 {
// https://www.rfc-editor.org/rfc/rfc4271#page-33
// If any recognized attribute has an Attribute Length that
// conflicts with the expected length (based on the attribute type
// code), then the Error Subcode MUST be set to Attribute Length
// Error. The Data field MUST contain the erroneous attribute
// (type, length, and value).
subcode = NOTIF_SUBCODE_ATTR_LEN_ERR
} else {
// https://www.rfc-editor.org/rfc/rfc4271#page-33
// If the ORIGIN attribute has an undefined value, then the Error
// Sub-code MUST be set to Invalid Origin Attribute. The Data field
// MUST contain the unrecognized attribute (type, length, and
// value).
subcode = NOTIF_SUBCODE_INVALID_ORIGIN_ATTR
}
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_ORIGIN,
Notification: &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
Subcode: subcode,
Data: notifDataForAttrBasedErr(PATH_ATTR_ORIGIN, b),
},
}
}
*o = OriginPathAttr(b[0])
return nil
}
func decodeUint32Set(b []byte) ([]uint32, error) {
if len(b) == 0 || len(b)%4 != 0 {
return nil, fmt.Errorf("invalid uint32 set len: %d", len(b))
}
ret := make([]uint32, 0, len(b)/4)
for len(b) > 0 {
ret = append(ret, binary.BigEndian.Uint32(b))
b = b[4:]
}
return ret, nil
}
func decodeLargeCommunitySet(b []byte) (LargeCommunitiesPathAttr, error) {
if len(b)%12 != 0 {
return nil, fmt.Errorf("invalid large community set len: %d", len(b))
}
ret := make([]LargeCommunity, 0, len(b)/12)
for len(b) >= 12 {
lc := LargeCommunity{
GlobalAdmin: binary.BigEndian.Uint32(b[:4]),
LocalData1: binary.BigEndian.Uint32(b[4:8]),
LocalData2: binary.BigEndian.Uint32(b[8:12]),
}
ret = append(ret, lc)
b = b[12:]
}
return ret, nil
}
func notifDataForAttrBasedErr(code uint8, attrData []byte) []byte {
nData := make([]byte, 0, 1+2+len(attrData))
nData = append(nData, code)
if len(attrData) > 255 {
extLen := make([]byte, 2)
binary.BigEndian.PutUint16(extLen, uint16(len(attrData)))
nData = append(nData, extLen...)
} else {
nData = append(nData, uint8(len(attrData)))
}
return append(nData, attrData...)
}
type ASPathAttr struct {
ASSet []uint32
ASSequence []uint32
}
func asPathMalformedErr() error {
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_AS_PATH,
Notification: &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
Subcode: NOTIF_SUBCODE_MALFORMED_AS_PATH,
},
}
}
func (a *ASPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_AS_PATH, b, false, true)
if err != nil {
return err
}
// empty AS-PATH
if len(b) == 0 {
return nil
}
if len(b) < 6 || len(b)%2 != 0 { // corebgp requires four octet AS
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_AS_PATH,
Notification: attrLenBadForCodeErr(PATH_ATTR_AS_PATH, b),
}
}
for len(b) > 0 {
// https://www.rfc-editor.org/rfc/rfc7606#page-11
// An AS_PATH is considered malformed if an unrecognized segment type is
// encountered or if it contains a malformed segment. A segment is
// considered malformed if any of the following are true:
//
// o There is an overrun where the Path Segment Length field of the
// last segment encountered would cause the Attribute Length to be
// exceeded.
//
// o There is an underrun where after the last successfully parsed
// segment there is only a single octet remaining (that is, there
// is not enough unconsumed data to provide even an empty segment
// header).
//
// o It has a Path Segment Length field of zero.
//
// An UPDATE message with a malformed AS_PATH attribute SHALL be
// handled using the approach of "treat-as-withdraw".
if len(b) < 6 || len(b)%2 != 0 {
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_AS_PATH,
Notification: attrLenBadForCodeErr(PATH_ATTR_AS_PATH, b),
}
}
segType := b[0]
// cast length to int otherwise it wraps the uint8/byte
segLen := int(b[1]) * 4
if segLen == 0 {
return asPathMalformedErr()
}
b = b[2:]
if len(b) < segLen {
return asPathMalformedErr()
}
set := b[:segLen]
if segType == 1 {
a.ASSet, err = decodeUint32Set(set)
if err != nil {
return asPathMalformedErr()
}
} else if segType == 2 {
a.ASSequence, err = decodeUint32Set(set)
if err != nil {
return asPathMalformedErr()
}
} else {
return asPathMalformedErr()
}
b = b[segLen:]
}
return nil
}
type NextHopPathAttr netip.Addr
func (n *NextHopPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_NEXT_HOP, b, false, true)
if err != nil {
return err
}
// https://www.rfc-editor.org/rfc/rfc7606#page-12
// The attribute is considered malformed if its length is not 4 [RFC4271].
//
// An UPDATE message with a malformed NEXT_HOP attribute SHALL be handled
// using the approach of "treat-as-withdraw".
if len(b) != 4 {
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_NEXT_HOP,
Notification: attrLenBadForCodeErr(PATH_ATTR_NEXT_HOP, b),
}
}
a, _ := netip.AddrFromSlice(b)
*n = NextHopPathAttr(a)
return nil
}
func decodeAddPathPrefixes(b []byte, ipv6 bool) ([]AddPathPrefix, error) {
if len(b) < 1 {
return nil, nil
}
prefixes := make([]AddPathPrefix, 0)
for len(b) > 0 {
if len(b) < 5 {
return nil, fmt.Errorf("invalid octets: %d for add path prefix ipv6: %v", len(b), ipv6)
}
var (
a AddPathPrefix
err error
)
a.ID = binary.BigEndian.Uint32(b)
b = b[4:]
a.Prefix, b, err = decodePrefix(b, ipv6)
if err != nil {
return nil, err
}
prefixes = append(prefixes, a)
}
return prefixes, nil
}
// decodePrefix decodes an IP prefix in b where prefix is of the form
// <length, prefix>. Length is expected to be a single octet indicating the
// length of the prefix in bits. Prefix contains the prefix followed by the
// minimum number of trailing bits needed to make the end fall on an octet
// boundary. If the address family of the prefix is IPv6, the ipv6 argument
// should be true, otherwise false.
func decodePrefix(b []byte, ipv6 bool) (netip.Prefix, []byte, error) {
if len(b) < 1 {
return netip.Prefix{}, nil, errors.New("prefix must be at least 1 byte")
}
bl := b[0]
if (!ipv6 && bl > 32) || (ipv6 && bl > 128) {
return netip.Prefix{}, nil, fmt.Errorf("invalid bit len: %d ipv6: %v", bl, ipv6)
}
b = b[1:]
octets := (bl + 7) / 8
if len(b) < int(octets) {
return netip.Prefix{}, nil, fmt.Errorf("invalid octets: %d for bit len: %d ipv6: %v", octets, bl, ipv6)
}
var addr netip.Addr
if ipv6 {
if octets > 16 {
return netip.Prefix{}, nil, errors.New("octets > 16 for IPv6 prefix")
}
var addr16 [16]byte
copy(addr16[:], b[:octets])
addr = netip.AddrFrom16(addr16)
} else {
if octets > 4 {
return netip.Prefix{}, nil, errors.New("octets > 4 for IPv4 prefix")
}
var addr4 [4]byte
copy(addr4[:], b[:octets])
addr = netip.AddrFrom4(addr4)
}
return netip.PrefixFrom(addr, int(bl)), b[octets:], nil
}
func decodePrefixes(b []byte, ipv6 bool) ([]netip.Prefix, error) {
if len(b) < 1 {
return nil, nil
}
prefixes := make([]netip.Prefix, 0)
for len(b) > 0 {
var (
p netip.Prefix
err error
)
p, b, err = decodePrefix(b, ipv6)
if err != nil {
return nil, err
}
prefixes = append(prefixes, p)
}
return prefixes, nil
}
type MEDPathAttr uint32
func (m *MEDPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_MED, b, true, false)
if err != nil {
return err
}
if len(b) != 4 {
// https://www.rfc-editor.org/rfc/rfc7606#page-12
// The attribute is considered malformed if its length is not 4
// [RFC4271].
//
// An UPDATE message with a malformed MULTI_EXIT_DISC attribute SHALL be
// handled using the approach of "treat-as-withdraw".
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_MED,
Notification: attrLenBadForCodeErr(PATH_ATTR_MED, b),
}
}
*m = MEDPathAttr(binary.BigEndian.Uint32(b))
return nil
}
type LocalPrefPathAttr uint32
func (l *LocalPrefPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_LOCAL_PREF, b, false, true)
if err != nil {
return err
}
if len(b) != 4 {
// https://www.rfc-editor.org/rfc/rfc7606#page-12
// The error handling of [RFC4271] is revised as follows:
//
// o if the LOCAL_PREF attribute is received from an external neighbor,
// it SHALL be discarded using the approach of "attribute discard";
// or
//
// o if received from an internal neighbor, it SHALL be considered
// malformed if its length is not equal to 4. If malformed, the
// UPDATE message SHALL be handled using the approach of "treat-as-
// withdraw".
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_LOCAL_PREF,
Notification: attrLenBadForCodeErr(PATH_ATTR_LOCAL_PREF, b),
}
}
*l = LocalPrefPathAttr(binary.BigEndian.Uint32(b))
return nil
}
type AtomicAggregatePathAttr bool
func (a *AtomicAggregatePathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_ATOMIC_AGGREGATE, b, true, true)
if err != nil {
return err
}
if len(b) != 0 {
// https://www.rfc-editor.org/rfc/rfc7606#page-12
// The attribute SHALL be considered malformed if its length is not 0
// [RFC4271].
//
// An UPDATE message with a malformed ATOMIC_AGGREGATE attribute SHALL
// be handled using the approach of "attribute discard".
return &AttrDiscardUpdateErr{
Code: PATH_ATTR_ATOMIC_AGGREGATE,
Notification: attrLenBadForCodeErr(PATH_ATTR_ATOMIC_AGGREGATE, b),
}
}
*a = true
return nil
}
type AggregatorPathAttr struct {
AS uint32
IP netip.Addr
}
func (a *AggregatorPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_AGGREGATOR, b, true, true)
if err != nil {
return err
}
if len(b) != 8 { // corebgp requires four octet AS
// https://www.rfc-editor.org/rfc/rfc7606#page-13
// The error conditions specified in [RFC4271] for the attribute are
// revised as follows:
//
// The AGGREGATOR attribute SHALL be considered malformed if any of the
// following applies:
//
// o Its length is not 6 (when the 4-octet AS number capability is not
// advertised to or not received from the peer [RFC6793]).
//
// o Its length is not 8 (when the 4-octet AS number capability is both
// advertised to and received from the peer).
//
// An UPDATE message with a malformed AGGREGATOR attribute SHALL be
// handled using the approach of "attribute discard".
return &AttrDiscardUpdateErr{
Code: PATH_ATTR_AGGREGATOR,
Notification: attrLenBadForCodeErr(PATH_ATTR_AGGREGATOR, b),
}
}
(*a).AS = binary.BigEndian.Uint32(b)
(*a).IP, _ = netip.AddrFromSlice(b[4:])
return nil
}
type LargeCommunity struct {
GlobalAdmin, LocalData1, LocalData2 uint32
}
type LargeCommunitiesPathAttr []LargeCommunity
func (l *LargeCommunitiesPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_LARGE_COMMUNITY, b, true, true)
if err != nil {
return err
}
if len(b) < 12 || len(b)%12 != 0 {
// https://www.rfc-editor.org/rfc/rfc8092#page-5
// The error handling of BGP Large Communities is as follows:
//
// o A BGP Large Communities attribute SHALL be considered malformed if
// the length of the BGP Large Communities Attribute value, expressed
// in octets, is not a non-zero multiple of 12.
//
// o A BGP Large Communities attribute SHALL NOT be considered
// malformed due to presence of duplicate Large Community values.
//
// o A BGP UPDATE message with a malformed BGP Large Communities
// attribute SHALL be handled using the approach of "treat-as-
// withdraw" as described in Section 2 of [RFC7606].
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_LARGE_COMMUNITY,
Notification: attrLenBadForCodeErr(PATH_ATTR_LARGE_COMMUNITY, b),
}
}
s, err := decodeLargeCommunitySet(b)
if err != nil {
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_LARGE_COMMUNITY,
Notification: attrLenBadForCodeErr(PATH_ATTR_LARGE_COMMUNITY, b),
}
}
*l = s
return nil
}
type CommunitiesPathAttr []uint32
func (c *CommunitiesPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_COMMUNITY, b, true, true)
if err != nil {
return err
}
if len(b) < 4 || len(b)%4 != 0 {
// https://www.rfc-editor.org/rfc/rfc7606#page-13
// The error handling of [RFC1997] is revised as follows:
//
// o The Community attribute SHALL be considered malformed if its
// length is not a non-zero multiple of 4.
//
// o An UPDATE message with a malformed Community attribute SHALL be
// handled using the approach of "treat-as-withdraw".
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_COMMUNITY,
Notification: attrLenBadForCodeErr(PATH_ATTR_COMMUNITY, b),
}
}
s, _ := decodeUint32Set(b)
*c = s
return nil
}
type OriginatorIDPathAttr netip.Addr
func (o *OriginatorIDPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_ORIGINATOR_ID, b, true, false)
if err != nil {
return err
}
if len(b) != 4 {
// https://www.rfc-editor.org/rfc/rfc7606#page-13
// The error handling of [RFC4456] is revised as follows:
//
// o if the ORIGINATOR_ID attribute is received from an external
// neighbor, it SHALL be discarded using the approach of "attribute
// discard"; or
//
// o if received from an internal neighbor, it SHALL be considered
// malformed if its length is not equal to 4. If malformed, the
// UPDATE message SHALL be handled using the approach of "treat-as-
// withdraw".
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_ORIGINATOR_ID,
Notification: attrLenBadForCodeErr(PATH_ATTR_ORIGINATOR_ID, b),
}
}
addr, _ := netip.AddrFromSlice(b)
*o = OriginatorIDPathAttr(addr)
return nil
}
type ClusterListPathAttr []netip.Addr
func (c *ClusterListPathAttr) Decode(flags PathAttrFlags, b []byte) error {
err := flags.Validate(PATH_ATTR_CLUSTER_LIST, b, true, false)
if err != nil {
return err
}
if len(b) < 4 || len(b)%4 != 0 {
// https://www.rfc-editor.org/rfc/rfc7606#page-13
// The error handling of [RFC4456] is revised as follows:
//
// o if the CLUSTER_LIST attribute is received from an external
// neighbor, it SHALL be discarded using the approach of "attribute
// discard"; or
//
// o if received from an internal neighbor, it SHALL be considered
// malformed if its length is not a non-zero multiple of 4. If
// malformed, the UPDATE message SHALL be handled using the approach
// of "treat-as-withdraw".
return &TreatAsWithdrawUpdateErr{
Code: PATH_ATTR_CLUSTER_LIST,
Notification: attrLenBadForCodeErr(PATH_ATTR_CLUSTER_LIST, b),
}
}
addrs := make([]netip.Addr, 0, len(b)/4)
for len(b) > 0 {
addr, _ := netip.AddrFromSlice(b[:4])
addrs = append(addrs, addr)
b = b[4:]
}
*c = addrs
return nil
}
func attrLenBadForCodeErr(code uint8, attrData []byte) *Notification {
return &Notification{
// https://www.rfc-editor.org/rfc/rfc4271#page-33
// If any recognized attribute has an Attribute Length that
// conflicts with the expected length (based on the
// attribute type code), then the Error Subcode MUST be set
// to Attribute Length Error. The Data field MUST contain
// the erroneous attribute (type, length, and value).
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
Subcode: NOTIF_SUBCODE_ATTR_LEN_ERR,
Data: notifDataForAttrBasedErr(code, attrData),
}
}
// AddPathPrefix is a prefix with an add-path ID.
// https://www.rfc-editor.org/rfc/rfc7911#section-3
type AddPathPrefix struct {
Prefix netip.Prefix
ID uint32
}
type DecodeFn[T any] func(t T, b []byte) error
// NewNLRIAddPathDecodeFn returns a DecodeFn to be used by an UpdateDecoder for
// decoding the NLRI field of an UPDATE message containing add-path prefixes.
// The closure fn will be passed type T and a slice of AddPathPrefix.
func NewNLRIAddPathDecodeFn[T any](fn func(t T, a []AddPathPrefix) error) DecodeFn[T] {
return func(t T, b []byte) error {
prefixes, err := decodeAddPathPrefixes(b, false)
if err != nil {
// https://www.rfc-editor.org/rfc/rfc4271#page-34
// The NLRI field in the UPDATE message is checked for syntactic
// validity. If the field is syntactically incorrect, then the
// Error Subcode MUST be set to Invalid Network Field.
return &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
Subcode: NOTIF_SUBCODE_INVALID_NETWORK_FIELD,
}
}
return fn(t, prefixes)
}
}
// NewNLRIDecodeFn returns a DecodeFn to be used by an UpdateDecoder for
// decoding the NLRI field of an UPDATE message. The closure fn will be passed
// type T and a slice of netip.Prefix.
func NewNLRIDecodeFn[T any](fn func(t T, p []netip.Prefix) error) DecodeFn[T] {
return func(t T, b []byte) error {
prefixes, err := decodePrefixes(b, false)
if err != nil {
// https://www.rfc-editor.org/rfc/rfc4271#page-34
// The NLRI field in the UPDATE message is checked for syntactic
// validity. If the field is syntactically incorrect, then the
// Error Subcode MUST be set to Invalid Network Field.
return &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
Subcode: NOTIF_SUBCODE_INVALID_NETWORK_FIELD,
}
}
return fn(t, prefixes)
}
}
// DecodeMPReachIPv6NextHops decodes one or two (RFC2545) IPv6 next hops
// contained in nh. Error handling is consistent with RFC7606.
func DecodeMPReachIPv6NextHops(nh []byte) ([]netip.Addr, error) {
if len(nh) != 16 && len(nh) != 32 {
// https://datatracker.ietf.org/doc/html/rfc2545#section-3
// The value of the Length of Next Hop Network Address field on a
// MP_REACH_NLRI attribute shall be set to 16, when only a global
// address is present, or 32 if a link-local address is also included in
// the Next Hop field.
//
// https://www.rfc-editor.org/rfc/rfc7606#page-14
// If the Length of Next Hop Network Address field of the MP_REACH
// attribute is inconsistent with that which was expected, the attribute
// is considered malformed. Since the next hop precedes the NLRI field
// in the attribute, in this case it will not be possible to reliably
// locate the NLRI; thus, the "session reset" or "AFI/SAFI disable"
// approach MUST be used.
return nil, &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
}
}
nhs := make([]netip.Addr, 0, len(nh)/16)
for len(nh) > 0 {
addr, _ := netip.AddrFromSlice(nh[:16])
nhs = append(nhs, addr)
nh = nh[16:]
}
return nhs, nil
}
// DecodeMPIPv6AddPathPrefixes decodes IPv6 add-path prefixes in b with
// multiprotocol error handling consistent with RFC7606.
func DecodeMPIPv6AddPathPrefixes(b []byte) ([]AddPathPrefix, error) {
prefixes, err := decodeAddPathPrefixes(b, true)
if err != nil {
// https://www.rfc-editor.org/rfc/rfc7606#page-7
// Finally, we observe that in order to use the approach of "treat-
// as-withdraw", the entire NLRI field and/or the MP_REACH_NLRI and
// MP_UNREACH_NLRI attributes need to be successfully parsed -- what
// this entails is discussed in more detail in Section 5. If this
// is not possible, the procedures of [RFC4271] and/or [RFC4760]
// continue to apply, meaning that the "session reset" approach (or
// the "AFI/SAFI disable" approach) MUST be followed.
return nil, &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
}
}
return prefixes, nil
}
// DecodeMPIPv6Prefixes decodes IPv6 prefixes in b with multiprotocol error
// handling consistent with RFC7606.
func DecodeMPIPv6Prefixes(b []byte) ([]netip.Prefix, error) {
prefixes, err := decodePrefixes(b, true)
if err != nil {
// https://www.rfc-editor.org/rfc/rfc7606#page-7
// Finally, we observe that in order to use the approach of "treat-
// as-withdraw", the entire NLRI field and/or the MP_REACH_NLRI and
// MP_UNREACH_NLRI attributes need to be successfully parsed -- what
// this entails is discussed in more detail in Section 5. If this
// is not possible, the procedures of [RFC4271] and/or [RFC4760]
// continue to apply, meaning that the "session reset" approach (or
// the "AFI/SAFI disable" approach) MUST be followed.
return nil, &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
}
}
return prefixes, nil
}
// NewWithdrawnAddPathRoutesDecodeFn returns a DecodeFn to be used by an
// UpdateDecoder for decoding the withdrawn routes field of an UPDATE message
// containing add-path prefixes. The closure fn will be passed type T and
// a slice of AddPathPrefix.
func NewWithdrawnAddPathRoutesDecodeFn[T any](fn func(t T, a []AddPathPrefix) error) DecodeFn[T] {
return func(t T, b []byte) error {
prefixes, err := decodeAddPathPrefixes(b, false)
if err != nil {
// Neither RFC4271 or RFC7606 define specific error handling for
// this case.
return &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
}
}
return fn(t, prefixes)
}
}
// NewWithdrawnRoutesDecodeFn returns a DecodeFn to be used by an UpdateDecoder
// for decoding the withdrawn routes field of an UPDATE message. The closure fn
// will be passed type T and a slice of netip.Prefix.
func NewWithdrawnRoutesDecodeFn[T any](fn func(t T, p []netip.Prefix) error) DecodeFn[T] {
return func(t T, b []byte) error {
prefixes, err := decodePrefixes(b, false)
if err != nil {
// Neither RFC4271 or RFC7606 define specific error handling for
// this case.
return &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
}
}
return fn(t, prefixes)
}
}
type MPPathAttrDecodeFn[T any] func(t T, flags PathAttrFlags, b []byte) error
// NewMPReachNLRIDecodeFn returns a MPPathAttrDecodeFn that can be used to
// compose logic for decoding a MP_REACH_NLRI path attribute through the
// provided closure fn. The closure fn will be passed type T, the afi, safi,
// next hop bytes, and nlri bytes.
func NewMPReachNLRIDecodeFn[T any](fn func(t T, afi uint16, safi uint8, nh, nlri []byte) error) MPPathAttrDecodeFn[T] {
return func(t T, flags PathAttrFlags, b []byte) error {
me := flags.Validate(PATH_ATTR_MP_REACH_NLRI, b, true, false)
if len(b) < 5 {
return errors.Join(me, mpLenErr())
}
afi := binary.BigEndian.Uint16(b)
safi := b[2]
nhLen := b[3]
b = b[4:]
if len(b) < int(nhLen)+1 { // reserved byte
return errors.Join(me, mpLenErr())
}
return errors.Join(me, fn(t, afi, safi, b[:nhLen], b[nhLen+1:]))
}
}
// NewMPUnreachNLRIDecodeFn returns a MPPathAttrDecodeFn that can be used to
// compose logic for decoding a MP_UNREACH_NLRI path attribute through the
// provided closure fn. The closure fn will be passed type T, the afi, safi,
// and withdrawn field for the path attribute.
func NewMPUnreachNLRIDecodeFn[T any](fn func(t T, afi uint16, safi uint8, withdrawn []byte) error) MPPathAttrDecodeFn[T] {
return func(t T, flags PathAttrFlags, b []byte) error {
me := flags.Validate(PATH_ATTR_MP_UNREACH_NLRI, b, true, false)
if len(b) < 3 {
return errors.Join(me, mpLenErr())
}
afi := binary.BigEndian.Uint16(b)
safi := b[2]
return errors.Join(me, fn(t, afi, safi, b[3:]))
}
}
func mpLenErr() *Notification {
// https://www.rfc-editor.org/rfc/rfc7606#page-14
// If the Length of Next Hop Network Address field of the MP_REACH
// attribute is inconsistent with that which was expected, the attribute
// is considered malformed. Since the next hop precedes the NLRI field
// in the attribute, in this case it will not be possible to reliably
// locate the NLRI; thus, the "session reset" or "AFI/SAFI disable"
// approach MUST be used.
return &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,
Subcode: NOTIF_SUBCODE_ATTR_LEN_ERR,
}
}
// PathAttrsDecodeFn is used by an instance of an UpdateDecoder to decode path
// attributes in an UPDATE message. It is invoked per-path attribute where
// code is the attribute code, flags are the attribute flags, and b contains the
// attribute data.
type PathAttrsDecodeFn[T any] func(t T, code uint8, flags PathAttrFlags, b []byte) error
// NewUpdateDecoder returns a new instance of an UpdateDecoder where wrFn is
// used to decode withdrawn routes, paFn is used to decode path attributes, and
// nlriFn is used to decode network layer reachability info.
func NewUpdateDecoder[T any](
wrFn DecodeFn[T],
paFn PathAttrsDecodeFn[T],
nlriFn DecodeFn[T],
) *UpdateDecoder[T] {
s := &UpdateDecoder[T]{
wrFn: wrFn,
paFn: paFn,
nlriFn: nlriFn,
}
return s
}
// UpdateDecoder decodes UPDATE messages. Type T can be passed to its underlying
// field-specific decode functions.
type UpdateDecoder[T any] struct {
wrFn DecodeFn[T]
paFn PathAttrsDecodeFn[T]
nlriFn DecodeFn[T]
}
func totalAttrLenErr(code uint8) error {
// https://www.rfc-editor.org/rfc/rfc7606#page-7
// There are two error cases in which the Total Attribute Length
// value can be in conflict with the enclosed path attributes, which
// themselves carry length values:
//
// o In the first case, the length of the last encountered path
// attribute would cause the Total Attribute Length to be
// exceeded when parsing the enclosed path attributes.
//
// o In the second case, fewer than three octets remain (or fewer
// than four octets, if the Attribute Flags field has the
// Extended Length bit set) when beginning to parse the
// attribute. That is, this case exists if there remains
// unconsumed data in the path attributes but yet insufficient
// data to encode a single minimum-sized path attribute.
//
// In either of these cases, an error condition exists and the
// "treat-as-withdraw" approach MUST be used (unless some other,
// more severe error is encountered dictating a stronger approach),
// and the Total Attribute Length MUST be relied upon to enable the
// beginning of the NLRI field to be located.
return &TreatAsWithdrawUpdateErr{
Code: code,
// RFC4271 does not define specific error handling for this case.
Notification: &Notification{
Code: NOTIF_CODE_UPDATE_MESSAGE_ERR,