/
bgp_proto.cc
1529 lines (1352 loc) · 52.3 KB
/
bgp_proto.cc
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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_proto.h"
#include "base/proto.h"
#include "base/logging.h"
#include "bgp/bgp_common.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_peer.h"
#include "bgp_server.h"
#include "net/bgp_af.h"
using boost::system::error_code;
using namespace std;
namespace mpl = boost::mpl;
BgpProto::OpenMessage::OpenMessage()
: BgpMessage(OPEN), as_num(-1), holdtime(-1), identifier(-1) {
}
BgpProto::OpenMessage::~OpenMessage() {
STLDeleteValues(&opt_params);
}
//
// Validate the capabilities in an incoming Open Message.
//
// Return the capability code that's missing if a problem is detected.
// Return 0 if capabilities in the message are OK.
//
// Note that we must not generate an error if we see a capability that we
// do not support.
//
// Go through all the capabilities and make sure that there's at least one
// MpExtension with an (afi, safi) pair that's also configured on the peer.
//
//
int BgpProto::OpenMessage::ValidateCapabilities(BgpPeer *peer) const {
bool mp_extension_ok = false;
// Go through each OptParam in the OpenMessage.
for (vector<OptParam *>::const_iterator param_it = opt_params.begin();
param_it != opt_params.end(); ++param_it) {
const OptParam *param = *param_it;
// Go through each Capability in the OptParam.
for (vector<Capability *>::const_iterator cap_it =
param->capabilities.begin();
cap_it != param->capabilities.end(); ++cap_it) {
const Capability *cap = *cap_it;
// See if the (afi,safi) in the MpExtension is configured on peer.
if (cap->code == Capability::MpExtension) {
const uint8_t *data = cap->capability.data();
uint16_t afi = get_value(data, 2);
uint8_t safi = get_value(data + 3, 1);
Address::Family family = BgpAf::AfiSafiToFamily(afi, safi);
if (peer->LookupFamily(family))
mp_extension_ok = true;
}
}
}
if (!mp_extension_ok)
return Capability::MpExtension;
return 0;
}
//
// Validate an incoming Open Message.
//
// Return one of the values from BgpProto::Notification::OpenMsgSubCode if
// an error is detected.
// Return 0 if message is OK.
//
int BgpProto::OpenMessage::Validate(BgpPeer *peer) const {
if (identifier == 0) {
BGP_LOG_PEER(Message, peer, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
BGP_PEER_DIR_IN, "Bad BGP Identifier: " << 0);
return BgpProto::Notification::BadBgpId;
}
if (identifier == peer->server()->bgp_identifier()) {
BGP_LOG_PEER(Message, peer, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
BGP_PEER_DIR_IN,
"Bad (Same as mine) BGP Identifier: " << identifier);
return BgpProto::Notification::BadBgpId;
}
if (as_num != peer->peer_as()) {
BGP_LOG_PEER(Message, peer, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
BGP_PEER_DIR_IN, "Bad Peer AS Number: " << as_num);
return BgpProto::Notification::BadPeerAS;
}
int result = ValidateCapabilities(peer);
if (result != 0) {
BGP_LOG_PEER(Message, peer, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
BGP_PEER_DIR_IN, "Unsupported Capability: " << result);
return BgpProto::Notification::UnsupportedCapability;
}
return 0;
}
const string BgpProto::OpenMessage::ToString() const {
std::ostringstream os;
error_code ec;
os << "AS " << as_num;
os << ", Hold Time " << holdtime;
os << ", Identifier " << Ip4Address(identifier).to_string(ec);
// Go through each OptParam in the OpenMessage.
for (vector<OptParam *>::const_iterator param_it = opt_params.begin();
param_it != opt_params.end(); ++param_it) {
const OptParam *param = *param_it;
// Go through each Capability in the OptParam.
vector<Capability *>::const_iterator cap_it =
param->capabilities.begin();
while (cap_it != param->capabilities.end()) {
const Capability *cap = *cap_it;
os << ", Code " << Capability::CapabilityToString(cap->code);
if (cap->code == Capability::MpExtension) {
const uint8_t *data = cap->capability.data();
uint16_t afi = get_value(data, 2);
uint8_t safi = get_value(data + 3, 1);
Address::Family family = BgpAf::AfiSafiToFamily(afi, safi);
os << " Family " << Address::FamilyToString(family);
}
if (++cap_it == param->capabilities.end())
break;
}
}
return os.str();
}
BgpProto::Notification::Notification()
: BgpMessage(NOTIFICATION), error(0), subcode(0) {
}
const std::string BgpProto::Notification::ToString() const {
return toString(static_cast<BgpProto::Notification::Code>(error), subcode);
}
const std::string BgpProto::Notification::toString(
BgpProto::Notification::Code code, int sub_code) {
std::string msg("");
switch (code) {
case MsgHdrErr:
msg += std::string("Message Header Error:") +
MsgHdrSubcodeToString(static_cast<MsgHdrSubCode>(sub_code));
break;
case OpenMsgErr:
msg += std::string("OPEN Message Error:") +
OpenMsgSubcodeToString(static_cast<OpenMsgSubCode>(sub_code));
break;
case UpdateMsgErr:
msg += std::string("UPDATE Message Error:") +
UpdateMsgSubCodeToString(static_cast<UpdateMsgSubCode>(sub_code));
break;
case HoldTimerExp:
msg += "Hold Timer Expired";
break;
case FSMErr:
msg += std::string("Finite State Machine Error:") +
FsmSubcodeToString(static_cast<FsmSubcode>(sub_code));
break;
case Cease:
msg += std::string("Cease:") +
CeaseSubcodeToString(static_cast<CeaseSubCode>(sub_code));
break;
default:
msg += "Unknown";
break;
}
return msg;
}
BgpProto::Keepalive::Keepalive()
: BgpMessage(KEEPALIVE) {
}
BgpProto::Update::Update()
: BgpMessage(UPDATE) {
}
BgpProto::Update::~Update() {
STLDeleteValues(&withdrawn_routes);
STLDeleteValues(&path_attributes);
STLDeleteValues(&nlri);
}
struct BgpAttrCodeCompare {
bool operator()(BgpAttribute *lhs, BgpAttribute *rhs) {
return lhs->code < rhs->code;
}
};
//Validate an incoming Update Message
//Returns 0 if message is OK
//Returns one of the values from enum UpdateMsgSubCode if an error is detected
int BgpProto::Update::Validate(const BgpPeer *peer, std::string &data) {
BgpAttrCodeCompare comp;
std::sort(path_attributes.begin(), path_attributes.end(), comp);
bool origin = false, nh = false, as_path = false, mp_reach_nlri = false,
local_pref = false;
bool ibgp = (peer->PeerType() == IBGP);
BgpAttrSpec::const_iterator it;
std::string rxed_attr("Path attributes : ");
for (it = path_attributes.begin(); it < path_attributes.end(); it++) {
if (it+1 < path_attributes.end() && (*it)->code == (*(it+1))->code) {
//duplicate attributes
return BgpProto::Notification::MalformedAttributeList;
}
rxed_attr += (*it)->ToString() + " ";
if ((*it)->code == BgpAttribute::Origin)
origin = true;
if ((*it)->code == BgpAttribute::LocalPref)
local_pref = true;
else if ((*it)->code == BgpAttribute::NextHop)
nh = true;
else if ((*it)->code == BgpAttribute::AsPath) {
as_path = true;
AsPathSpec *asp = static_cast<AsPathSpec *>(*it);
// Check segments size for ebpg,
// IBGP can have empty path for routes that are originated
if (!ibgp) {
if (!asp->path_segments.size() ||
!asp->path_segments[0]->path_segment.size())
return BgpProto::Notification::MalformedASPath;
}
} else if ((*it)->code == BgpAttribute::MPReachNlri)
mp_reach_nlri = true;
}
BGP_LOG_PEER(Message, const_cast<BgpPeer *>(peer),
SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
BGP_PEER_DIR_IN, rxed_attr);
if (nlri.size() > 0 && !nh) {
// next-hop attribute must be present if IPv4 NLRI is present
char attrib_type = BgpAttribute::NextHop;
data = std::string(&attrib_type, 1);
return BgpProto::Notification::MissingWellKnownAttrib;
}
if (nlri.size() > 0 || mp_reach_nlri) {
// origin and as_path must be present if any NLRI is present
if (!origin) {
char attrib_type = BgpAttribute::Origin;
data = std::string(&attrib_type, 1);
return BgpProto::Notification::MissingWellKnownAttrib;
}
if (!as_path) {
char attrib_type = BgpAttribute::AsPath;
data = std::string(&attrib_type, 1);
return BgpProto::Notification::MissingWellKnownAttrib;
}
// All validations for IBGP
if (ibgp && !local_pref) {
// If IBGP, local_pref is mandatory
char attrib_type = BgpAttribute::LocalPref;
data = std::string(&attrib_type, 1);
return BgpProto::Notification::MissingWellKnownAttrib;
}
}
return 0;
}
int BgpProto::Update::CompareTo(const BgpProto::Update &rhs) const{
KEY_COMPARE(withdrawn_routes.size(), rhs.withdrawn_routes.size());
for (size_t i=0; i < withdrawn_routes.size(); i++) {
KEY_COMPARE(withdrawn_routes[i]->prefixlen,
rhs.withdrawn_routes[i]->prefixlen);
KEY_COMPARE(withdrawn_routes[i]->prefix,
rhs.withdrawn_routes[i]->prefix);
}
KEY_COMPARE(path_attributes.size(), rhs.path_attributes.size());
for (size_t i = 0; i < rhs.path_attributes.size(); i++) {
int ret = path_attributes[i]->CompareTo(*rhs.path_attributes[i]);
if (ret != 0) {
cout << "Unequal " << TYPE_NAME(*path_attributes[i]) << endl;
return ret;
}
}
KEY_COMPARE(nlri.size(), rhs.nlri.size());
for (size_t i=0; i<rhs.nlri.size(); i++) {
KEY_COMPARE(nlri[i]->prefixlen, rhs.nlri[i]->prefixlen);
KEY_COMPARE(nlri[i]->prefix, rhs.nlri[i]->prefix);
}
return 0;
}
class BgpMarker : public ProtoElement<BgpMarker> {
public:
static const int kSize = 16;
static const int kErrorCode = BgpProto::Notification::MsgHdrErr;
static const int kErrorSubcode = BgpProto::Notification::ConnNotSync;
static bool Verifier(const void *obj, const uint8_t *data, size_t size,
ParseContext *context) {
for (int i = 0; i < 16; i++) {
if (data[i] != 0xff) return false;
}
return true;
}
static void Writer(const void *msg, uint8_t *data, size_t size) {
for (int i = 0; i < 16; i++) {
data[i] = 0xff;
}
}
};
class BgpMsgLength : public ProtoElement<BgpMsgLength> {
public:
static const int kSize = 2;
static const int kErrorCode = BgpProto::Notification::MsgHdrErr;
static const int kErrorSubcode = BgpProto::Notification::BadMsgLength;
struct Offset {
std::string operator()() {
return "BgpMsgLength";
}
};
typedef Offset SaveOffset;
static bool Verifier(const void *obj, const uint8_t *data, size_t size,
ParseContext *context) {
int value = get_short(data);
if (value < BgpProto::kMinMessageSize ||
value > BgpProto::kMaxMessageSize) {
return false;
}
if ((size_t) value < context->offset() + size) {
return false;
}
return true;
}
struct SetLength {
static void Callback(EncodeContext *context, uint8_t *data,
int offset, int element_size) {
put_value(data, kSize, context->length());
}
};
typedef SetLength EncodingCallback;
};
// BGP OPEN
class BgpOpenVersion : public ProtoElement<BgpOpenVersion> {
public:
static const int kSize = 1;
static const int kErrorCode = BgpProto::Notification::OpenMsgErr;
static const int kErrorSubcode = BgpProto::Notification::UnsupportedVersion;
static bool Verifier(const void * obj, const uint8_t *data, size_t size,
ParseContext *context) {
return data[0] == 0x4;
}
static void Writer(const void *msg, uint8_t *data, size_t size) {
*data = 0x4;
}
};
class BgpOpenAsNum : public ProtoElement<BgpOpenAsNum> {
public:
static const int kSize = 2;
typedef Accessor<BgpProto::OpenMessage, uint32_t,
&BgpProto::OpenMessage::as_num> Setter;
};
class BgpHoldTime : public ProtoElement<BgpHoldTime> {
public:
static const int kSize = 2;
static const int kErrorCode = BgpProto::Notification::OpenMsgErr;
static const int kErrorSubcode =
BgpProto::Notification::UnacceptableHoldTime;
static bool Verifier(const BgpProto::OpenMessage *obj, const uint8_t *data,
size_t size, ParseContext *context) {
uint16_t value = get_value(data, 2);
return (value == 0 || value >= 3);
}
typedef Accessor<BgpProto::OpenMessage, int32_t,
&BgpProto::OpenMessage::holdtime> Setter;
};
class BgpIdentifier : public ProtoElement<BgpIdentifier> {
public:
static const int kSize = 4;
typedef Accessor<BgpProto::OpenMessage, uint32_t,
&BgpProto::OpenMessage::identifier> Setter;
};
class BgpOpenCapabilityCode : public ProtoElement<BgpOpenCapabilityCode> {
public:
static const int kSize = 1;
typedef Accessor<BgpProto::OpenMessage::Capability, int,
&BgpProto::OpenMessage::Capability::code> Setter;
};
class BgpOpenCapabilityLength : public ProtoElement<BgpOpenCapabilityLength> {
public:
static const int kSize = 1;
typedef int SequenceLength;
};
class BgpOpenCapabilityValue : public ProtoElement<BgpOpenCapabilityValue> {
public:
static const int kSize = -1;
typedef VectorAccessor<BgpProto::OpenMessage::Capability, uint8_t,
&BgpProto::OpenMessage::Capability::capability> Setter;
};
class BgpOpenCapability : public ProtoSequence<BgpOpenCapability> {
public:
typedef mpl::list<BgpOpenCapabilityCode, BgpOpenCapabilityLength,
BgpOpenCapabilityValue> Sequence;
};
class BgpOpenCapabilities : public ProtoSequence<BgpOpenCapabilities> {
public:
static const int kSize = 1;
static const int kMinOccurs = 0;
static const int kMaxOccurs = -1;
typedef mpl::list<BgpOpenCapability> Sequence;
typedef CollectionAccessor<BgpProto::OpenMessage::OptParam,
vector<BgpProto::OpenMessage::Capability *>,
&BgpProto::OpenMessage::OptParam::capabilities> ContextStorer;
struct OptMatch {
bool match(const BgpProto::OpenMessage::OptParam *ctx) {
return !ctx->capabilities.empty();
}
};
typedef OptMatch ContextMatch;
};
class BgpOpenOptParamChoice : public ProtoChoice<BgpOpenOptParamChoice> {
public:
static const int kSize = 1;
static const int kErrorCode = BgpProto::Notification::OpenMsgErr;
static const int kErrorSubcode =
BgpProto::Notification::UnsupportedOptionalParam;
typedef mpl::map<
mpl::pair<mpl::int_<BgpProto::OpenMessage::OPEN_OPT_CAPABILITIES>,
BgpOpenCapabilities>
> Choice;
};
class BgpOpenOptParam : public ProtoSequence<BgpOpenOptParam> {
public:
static const int kSize = 1;
static const int kMinOccurs = 0;
static const int kMaxOccurs = -1;
typedef CollectionAccessor<BgpProto::OpenMessage,
vector<BgpProto::OpenMessage::OptParam *>,
&BgpProto::OpenMessage::opt_params> ContextStorer;
typedef mpl::list<BgpOpenOptParamChoice> Sequence;
};
class BgpOpenMessage : public ProtoSequence<BgpOpenMessage> {
public:
typedef mpl::list<BgpOpenVersion,
BgpOpenAsNum,
BgpHoldTime,
BgpIdentifier,
BgpOpenOptParam> Sequence;
typedef BgpProto::OpenMessage ContextType;
};
class NotificationErrorCode : public ProtoElement<NotificationErrorCode> {
public:
static const int kSize = 1;
typedef Accessor<BgpProto::Notification, int,
&BgpProto::Notification::error> Setter;
};
class NotificationErrorSubcode : public ProtoElement<NotificationErrorSubcode> {
public:
static const int kSize = 1;
typedef Accessor<BgpProto::Notification, int,
&BgpProto::Notification::subcode> Setter;
};
class NotificationData : public ProtoElement<NotificationData> {
public:
static const int kSize = -1;
typedef Accessor<BgpProto::Notification, string,
&BgpProto::Notification::data> Setter;
};
class BgpNotificationMessage : public ProtoSequence<BgpNotificationMessage> {
public:
typedef mpl::list<NotificationErrorCode,
NotificationErrorSubcode,
NotificationData> Sequence;
typedef BgpProto::Notification ContextType;
};
class BgpKeepaliveMessage : public ProtoSequence<BgpKeepaliveMessage> {
public:
typedef mpl::list<> Sequence;
typedef BgpProto::Keepalive ContextType;
};
class BgpPrefixLen : public ProtoElement<BgpPrefixLen> {
public:
static const int kSize = 1;
struct PrefixLen {
int operator()(BgpProtoPrefix *obj, const uint8_t *data, size_t size) {
int bits = data[0];
return (bits + 7) / 8;
}
};
typedef PrefixLen SequenceLength;
typedef Accessor<BgpProtoPrefix, int,
&BgpProtoPrefix::prefixlen> Setter;
};
class BgpPrefixAddress : public ProtoElement<BgpPrefixAddress> {
public:
static const int kSize = -1;
typedef VectorAccessor<BgpProtoPrefix, uint8_t,
&BgpProtoPrefix::prefix> Setter;
};
class BgpUpdateWithdrawnRoutes :
public ProtoSequence<BgpUpdateWithdrawnRoutes> {
public:
static const int kSize = 2;
static const int kMinOccurs = 0;
static const int kMaxOccurs = -1;
static const int kErrorCode = BgpProto::Notification::UpdateMsgErr;
static const int kErrorSubcode =
BgpProto::Notification::MalformedAttributeList;
typedef CollectionAccessor<BgpProto::Update,
vector<BgpProtoPrefix *>,
&BgpProto::Update::withdrawn_routes> ContextStorer;
struct OptMatch {
bool match(const BgpProto::Update *ctx) {
return !ctx->withdrawn_routes.empty();
}
};
typedef OptMatch ContextMatch;
typedef mpl::list<BgpPrefixLen, BgpPrefixAddress> Sequence;
};
class BgpPathAttrLength : public ProtoElement<BgpPathAttrLength> {
public:
static const int kSize = -1;
struct AttrLen {
int operator()(BgpAttribute *obj,
const uint8_t *data, size_t &size) {
if (obj->flags & BgpAttribute::ExtendedLength) {
// Extended Length: use 2 bytes to read
size = 2;
} else {
size = 1;
}
return get_value(data, size);
}
};
typedef AttrLen SequenceLength;
struct AttrSizeSet {
static int get(const BgpAttribute *obj) {
if (obj->GetEncodeFlags() & BgpAttribute::ExtendedLength) {
// Extended Length: use 2 bytes to read
return 2;
} else {
return 1;
}
}
};
typedef AttrSizeSet SizeSetter;
};
template <class Derived>
struct BgpContextSwap {
Derived *operator()(const BgpAttribute *attr) {
return new Derived(*attr);
}
};
template <class C>
struct BgpAttributeVerifier {
static bool Verifier(const C *obj, const uint8_t *data, size_t size,
ParseContext *context) {
int pre = (obj->flags & BgpAttribute::ExtendedLength) ? 4 : 3;
if (C::kSize > 0 && (int) context->total_size() != C::kSize) {
int offset = pre + context->total_size() - context->size();
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::AttribLengthError,
TYPE_NAME(C), data - offset, context->total_size() + pre);
return false;
}
if ((obj->flags & BgpAttribute::FLAG_MASK) != C::kFlags) {
int offset = pre + context->total_size() - context->size();
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::AttribFlagsError,
TYPE_NAME(C), data - offset, context->total_size() + pre);
return false;
}
return true;
}
};
template <>
struct BgpAttributeVerifier<BgpAttrOrigin> {
static bool Verifier(const BgpAttrOrigin *obj, const uint8_t *data,
size_t size, ParseContext *context) {
int pre = (obj->flags & BgpAttribute::ExtendedLength) ? 4 : 3;
if (context->size() != 1) {
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::AttribLengthError,
"BgpAttrOrigin", data - pre, size + pre);
return false;
}
if ((obj->flags & BgpAttribute::FLAG_MASK) != BgpAttrOrigin::kFlags) {
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::AttribFlagsError,
"BgpAttrOrigin", data - pre, size + pre);
return false;
}
uint8_t value = data[0];
if (value != BgpAttrOrigin::IGP && value != BgpAttrOrigin::EGP &&
value != BgpAttrOrigin::INCOMPLETE) {
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::InvalidOrigin,
"BgpAttrOrigin", data - pre, size + pre);
return false;
}
return true;
}
};
template <>
struct BgpAttributeVerifier<BgpAttrNextHop> {
static bool Verifier(const BgpAttrNextHop *obj, const uint8_t *data,
size_t size, ParseContext *context) {
int pre = (obj->flags & BgpAttribute::ExtendedLength) ? 4 : 3;
if ((int)context->size() != BgpAttrNextHop::kSize) {
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::AttribLengthError,
"BgpAttrNextHop", data - pre, size + pre);
return false;
}
if ((obj->flags & BgpAttribute::FLAG_MASK) != BgpAttrNextHop::kFlags) {
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::AttribFlagsError,
"BgpAttrNextHop", data - pre, size + pre);
return false;
}
uint32_t value = get_value(data, size);
//TODO: More checks are needed
if (value == 0) {
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::InvalidNH,
"BgpAttrNextHop", data - pre, size + pre);
return false;
}
return true;
}
};
template <>
struct BgpAttributeVerifier<AsPathSpec::PathSegment> {
static bool Verifier(const AsPathSpec::PathSegment *obj, const uint8_t *data,
size_t size, ParseContext *context) {
return true;
}
};
template <int Size, class C, typename T, T C::*Member>
class BgpAttributeValue :
public ProtoElement<BgpAttributeValue<Size, C, T, Member> > {
public:
static const int kSize = Size;
static bool Verifier(const C *obj, const uint8_t *data, size_t size,
ParseContext *context) {
return BgpAttributeVerifier<C>::Verifier(obj, data, size, context);
}
typedef Accessor<C, T, Member> Setter;
};
template <class C, int Size, typename T, T C::*M>
class BgpAttrTemplate :
public ProtoSequence<BgpAttrTemplate<C, Size, T, M> > {
public:
static const int kErrorCode = BgpProto::Notification::UpdateMsgErr;
static const int kErrorSubcode = BgpProto::Notification::AttribLengthError;
typedef C ContextType;
typedef BgpContextSwap<C> ContextSwap;
typedef mpl::list<BgpPathAttrLength, BgpAttributeValue<Size, C, T, M>
> Sequence;
};
class BgpPathAttributeAtomicAggregate :
public ProtoSequence<BgpPathAttributeAtomicAggregate> {
public:
static bool Verifier(const BgpAttrAtomicAggregate * obj,
const uint8_t *data, size_t size,
ParseContext *context) {
if (data[0] != 0) {
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::AttribLengthError,
"BgpAttrAtomicAggregate", data - 2, data[0] + 3);
return false;
}
if (obj->flags != BgpAttrAtomicAggregate::kFlags) {
context->SetError(BgpProto::Notification::UpdateMsgErr,
BgpProto::Notification::AttribFlagsError,
"BgpAttrAtomicAggregate", data - 2, 3);
return false;
}
return true;
}
typedef BgpAttrAtomicAggregate ContextType;
typedef BgpContextSwap<BgpAttrAtomicAggregate> ContextSwap;
typedef mpl::list<BgpPathAttrLength> Sequence;
};
class BgpPathAttributeAggregator :
public ProtoSequence<BgpPathAttributeAggregator> {
public:
typedef BgpAttrAggregator ContextType;
typedef BgpContextSwap<BgpAttrAggregator> ContextSwap;
typedef mpl::list<BgpPathAttrLength,
BgpAttributeValue<2, BgpAttrAggregator, as_t,
&BgpAttrAggregator::as_num>,
BgpAttributeValue<4, BgpAttrAggregator, uint32_t,
&BgpAttrAggregator::address>
> Sequence;
};
class BgpPathAttrAsPathSegmentLength :
public ProtoElement<BgpPathAttrAsPathSegmentLength> {
public:
static const int kSize = 1;
struct PathSegmentLength {
int operator()(AsPathSpec::PathSegment *obj,
const uint8_t *data, size_t size) {
return get_value(data, 1) * 2;
}
};
typedef PathSegmentLength SequenceLength;
struct SetLength {
static void Callback(EncodeContext *context, uint8_t *data,
int offset, int element_size) {
int len = get_value(data, kSize);
put_value(data, kSize, len/2);
}
};
typedef SetLength EncodingCallback;
};
class BgpPathAttrAsPathSegmentValue :
public ProtoElement<BgpPathAttrAsPathSegmentValue> {
public:
static const int kSize = -1;
typedef VectorAccessor<AsPathSpec::PathSegment, as_t,
&AsPathSpec::PathSegment::path_segment> Setter;
};
class BgpPathAttrAsPathSegmentList :
public ProtoSequence<BgpPathAttrAsPathSegmentList> {
public:
static const int kMinOccurs = 0;
static const int kMaxOccurs = -1;
static bool Verifier(const AsPathSpec *obj, const uint8_t *data,
size_t size, ParseContext *context) {
return BgpAttributeVerifier<AsPathSpec>::Verifier(obj, data, size,
context);
}
typedef CollectionAccessor<AsPathSpec,
vector<AsPathSpec::PathSegment *>,
&AsPathSpec::path_segments> ContextStorer;
typedef mpl::list<BgpAttributeValue<1, AsPathSpec::PathSegment, int,
&AsPathSpec::PathSegment::path_segment_type>,
BgpPathAttrAsPathSegmentLength,
BgpPathAttrAsPathSegmentValue
> Sequence;
};
class BgpPathAttributeAsPath : public ProtoSequence<BgpPathAttributeAsPath> {
public:
typedef AsPathSpec ContextType;
typedef BgpContextSwap<AsPathSpec> ContextSwap;
typedef mpl::list<BgpPathAttrLength, BgpPathAttrAsPathSegmentList> Sequence;
};
class BgpPathAttributeFlags : public ProtoElement<BgpPathAttributeFlags> {
public:
static const int kSize = 1;
struct FlagsAccessor {
static void set(BgpAttribute *obj, uint8_t value) {
obj->flags = value;
}
static uint8_t get(const BgpAttribute *obj) {
return obj->GetEncodeFlags();
}
};
typedef FlagsAccessor Setter;
};
class BgpPathAttributeCommunityList :
public ProtoElement<BgpPathAttributeCommunityList> {
public:
static const int kSize = -1;
static bool Verifier(const CommunitySpec *obj, const uint8_t *data,
size_t size, ParseContext *context) {
return BgpAttributeVerifier<CommunitySpec>::Verifier(obj, data, size,
context);
}
typedef VectorAccessor<CommunitySpec, uint32_t,
&CommunitySpec::communities> Setter;
};
class BgpPathAttributeCommunities :
public ProtoSequence<BgpPathAttributeCommunities> {
public:
typedef CommunitySpec ContextType;
typedef BgpContextSwap<CommunitySpec> ContextSwap;
typedef mpl::list<BgpPathAttrLength,
BgpPathAttributeCommunityList> Sequence;
};
class BgpPathAttributeExtendedCommunityList :
public ProtoElement<BgpPathAttributeExtendedCommunityList> {
public:
static const int kSize = -1;
static bool Verifier(const ExtCommunitySpec *obj, const uint8_t *data,
size_t size, ParseContext *context) {
return BgpAttributeVerifier<ExtCommunitySpec>::Verifier(obj, data, size,
context);
}
typedef VectorAccessor<ExtCommunitySpec, uint64_t,
&ExtCommunitySpec::communities> Setter;
};
class BgpPathAttributeExtendedCommunities :
public ProtoSequence<BgpPathAttributeExtendedCommunities> {
public:
typedef ExtCommunitySpec ContextType;
typedef BgpContextSwap<ExtCommunitySpec> ContextSwap;
typedef mpl::list<BgpPathAttrLength,
BgpPathAttributeExtendedCommunityList> Sequence;
};
class BgpPathAttributePmsiTunnelIdentifier :
public ProtoElement<BgpPathAttributePmsiTunnelIdentifier> {
public:
static const int kSize = -1;
static bool Verifier(const PmsiTunnelSpec *obj, const uint8_t *data,
size_t size, ParseContext *context) {
return BgpAttributeVerifier<PmsiTunnelSpec>::Verifier(
obj, data, size, context);
}
typedef VectorAccessor<PmsiTunnelSpec, uint8_t,
&PmsiTunnelSpec::identifier> Setter;
};
class BgpPathAttributePmsiTunnel :
public ProtoSequence<BgpPathAttributePmsiTunnel> {
public:
typedef PmsiTunnelSpec ContextType;
typedef BgpContextSwap<PmsiTunnelSpec> ContextSwap;
typedef mpl::list<BgpPathAttrLength,
BgpAttributeValue<1, PmsiTunnelSpec, uint8_t, &PmsiTunnelSpec::tunnel_flags>,
BgpAttributeValue<1, PmsiTunnelSpec, uint8_t, &PmsiTunnelSpec::tunnel_type>,
BgpAttributeValue<3, PmsiTunnelSpec, uint32_t, &PmsiTunnelSpec::label>,
BgpPathAttributePmsiTunnelIdentifier> Sequence;
};
class BgpPathAttributeDiscoveryEdgeAddressLen :
public ProtoElement<BgpPathAttributeDiscoveryEdgeAddressLen> {
public:
static const int kSize = 1;
static bool Verifier(const void *obj, const uint8_t *data,
size_t size, ParseContext *context) {
uint8_t value = get_value(data, 1);
return (value == 4);
}
typedef int SequenceLength;
};
class BgpPathAttributeDiscoveryEdgeAddressValue :
public ProtoElement<BgpPathAttributeDiscoveryEdgeAddressValue> {
public:
static const int kSize = -1;
typedef VectorAccessor<EdgeDiscoverySpec::Edge, uint8_t,
&EdgeDiscoverySpec::Edge::address> Setter;
};
class BgpPathAttributeDiscoveryEdgeAddress :
public ProtoSequence<BgpPathAttributeDiscoveryEdgeAddress> {
public:
typedef mpl::list<BgpPathAttributeDiscoveryEdgeAddressLen,
BgpPathAttributeDiscoveryEdgeAddressValue> Sequence;
};
class BgpPathAttributeDiscoveryEdgeLabelLen :
public ProtoElement<BgpPathAttributeDiscoveryEdgeLabelLen> {
public:
static const int kSize = 1;
static bool Verifier(const void *obj, const uint8_t *data,
size_t size, ParseContext *context) {
uint8_t value = get_value(data, 1);
return (value > 0 && value % 8 == 0);
}
typedef int SequenceLength;
};
class BgpPathAttributeDiscoveryEdgeLabelValues :
public ProtoElement<BgpPathAttributeDiscoveryEdgeLabelValues> {
public:
static const int kSize = -1;
typedef VectorAccessor<EdgeDiscoverySpec::Edge, uint32_t,
&EdgeDiscoverySpec::Edge::labels> Setter;
};
class BgpPathAttributeDiscoveryEdgeLabels :
public ProtoSequence<BgpPathAttributeDiscoveryEdgeLabels> {
public:
typedef mpl::list<BgpPathAttributeDiscoveryEdgeLabelLen,
BgpPathAttributeDiscoveryEdgeLabelValues> Sequence;
};
class BgpPathAttributeDiscoveryEdgeList :
public ProtoSequence<BgpPathAttributeDiscoveryEdgeList> {
public:
static const int kMinOccurs = 1;
static const int kMaxOccurs = -1;
static bool Verifier(const EdgeDiscoverySpec *obj, const uint8_t *data,
size_t size, ParseContext *context) {
return BgpAttributeVerifier<EdgeDiscoverySpec>::Verifier(
obj, data, size, context);
}
typedef CollectionAccessor<EdgeDiscoverySpec,
vector<EdgeDiscoverySpec::Edge *>,
&EdgeDiscoverySpec::edge_list> ContextStorer;
typedef mpl::list<BgpPathAttributeDiscoveryEdgeAddress,
BgpPathAttributeDiscoveryEdgeLabels> Sequence;
};
class BgpPathAttributeEdgeDiscovery :
public ProtoSequence<BgpPathAttributeEdgeDiscovery> {
public:
typedef EdgeDiscoverySpec ContextType;
typedef BgpContextSwap<EdgeDiscoverySpec> ContextSwap;
typedef mpl::list<BgpPathAttrLength,
BgpPathAttributeDiscoveryEdgeList> Sequence;
};
class BgpPathAttributeForwardingEdgeLen :
public ProtoElement<BgpPathAttributeForwardingEdgeLen> {
public:
static const int kSize = 1;
static bool Verifier(const void *obj, const uint8_t *data,
size_t size, ParseContext *context) {
uint8_t value = get_value(data, 1);
return (value == 4);
}
struct GetLength {
int operator()(EdgeForwardingSpec::Edge *obj,
const uint8_t *data, size_t size) {
obj->address_len = get_value(data, 1) * 2 + 8;
return obj->address_len;
}
};
typedef GetLength SequenceLength;
struct SetLength {
static void Callback(EncodeContext *context, uint8_t *data,
int offset, int element_size) {
int len = get_value(data, kSize);
put_value(data, kSize, (len - 8) / 2);
}
};
typedef SetLength EncodingCallback;