OpenSDN source code
bgp_proto.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef SRC_BGP_BGP_PROTO_H_
7 #define SRC_BGP_BGP_PROTO_H_
8 
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 
13 #include "base/parse_object.h"
14 #include "bgp/bgp_attr.h"
15 #include "bgp/community.h"
16 
17 struct BgpMpNlri;
18 class BgpPeer;
19 
20 class BgpProto {
21 public:
22  enum MessageType {
23  OPEN = 1,
24  UPDATE = 2,
26  KEEPALIVE = 4
27  };
28 
29  enum BgpPeerType {
33  };
34 
35  static std::string BgpPeerTypeString(BgpPeerType peer_type) {
36  switch (peer_type) {
37  case IBGP:
38  return "IBGP";
39  break;
40  case EBGP:
41  return "EBGP";
42  break;
43  case XMPP:
44  return "XMPP";
45  break;
46  }
47  assert(false);
48  return "OTHER";
49  }
50 
51  struct BgpMessage : public ParseObject {
53  }
55  virtual const std::string ToString() const { return ""; }
56  };
57 
58  struct OpenMessage : public BgpMessage {
61  };
62 
63  OpenMessage();
64  ~OpenMessage();
65  int Validate(BgpPeer *peer) const;
66  uint32_t as_num;
67  int32_t holdtime;
68  uint32_t identifier;
69 
70  struct Capability : public ParseObject {
72  Reserved = 0,
79  AS4Support = 65,
80  Dynamic = 67,
82  AddPath = 69,
85  RouteRefreshCisco = 128
86  };
87  static std::string CapabilityToString(int capability) {
88  switch (capability) {
89  case Reserved:
90  return "Reserved";
91  case MpExtension:
92  return "MpExtension";
93  case RouteRefresh:
94  return "RouteRefresh";
96  return "OutboundRouteFiltering";
98  return "MultipleRoutesToADestination";
99  case ExtendedNextHop:
100  return "ExtendedNextHop";
101  case GracefulRestart:
102  return "GracefulRestart";
103  case AS4Support:
104  return "AS4Support";
105  case Dynamic:
106  return "Dynamic";
107  case MultisessionBgp:
108  return "MultisessionBgp";
109  case AddPath:
110  return "AddPath";
112  return "EnhancedRouteRefresh";
114  return "LongLivedGracefulRestart";
115  case RouteRefreshCisco:
116  return "RouteRefreshCisco";
117  }
118 
119  std::ostringstream oss;
120  oss << "Unknown(" << capability << ")";
121  return oss.str();
122  }
124  explicit Capability(int code, const uint8_t *src, int size) :
125  code(code), capability(src, src + size) {}
126 
127  struct GR {
128  enum Flags {
130  RestartedFlag = 0x8000,
132  RestartTimeMask = 0x0FFF,
133  };
134  explicit GR() { Initialize(); }
135  void Initialize() {
136  flags = 0;
137  time = 0;
138  families.clear();
139  }
140  struct Family {
141  Family(uint16_t afi, uint8_t safi, uint8_t flags) :
142  afi(afi), safi(safi), flags(flags) { }
143  uint16_t afi;
144  uint8_t safi;
145  uint8_t flags;
146 
148  return (flags & ForwardingStatePreservedFlag) != 0;
149  }
150  };
151  static Capability *Encode(uint16_t gr_time, bool restarted,
152  bool notification,
153  const std::vector<uint8_t> &gr_afi_flags,
154  const std::vector<Address::Family> &gr_families);
155  static bool Decode(GR *gr_params,
156  const std::vector<Capability *> &capabilities);
157  static void GetFamilies(const GR &gr_params,
158  std::vector<std::string> *families);
159  bool restarted() const { return (flags & RestartedFlag) != 0; }
160  bool notification() const {
161  return (flags & NotificationFlag) != 0;
162  }
163  void set_flags(uint16_t gr_cap_bytes) {
164  flags = gr_cap_bytes & ~RestartTimeMask;
165  }
166  void set_time(uint16_t gr_cap_bytes) {
167  time = gr_cap_bytes & RestartTimeMask;
168  }
169  uint16_t flags;
170  uint16_t time;
171  std::vector<Family> families;
172  };
173 
174  struct LLGR {
175  enum {
177  RestartTimeMask = 0x00FFFFFF,
178  };
179  explicit LLGR() { Initialize(); }
180  void Initialize() {
181  time = 0;
182  families.clear();
183  }
184  struct Family {
185  Family(uint16_t afi, uint8_t safi, uint8_t flags,
186  uint32_t time) :
187  afi(afi), safi(safi), flags(flags), time(time) {
188  }
189  uint16_t afi;
190  uint8_t safi;
191  uint8_t flags;
192  uint32_t time; // 24 bits only
193 
195  return (flags & ForwardingStatePreservedFlag) != 0;
196  }
197  };
198 
199  static Capability *Encode(uint32_t llgr_time,
200  uint8_t llgr_afi_flags,
201  const std::vector<Address::Family> &llgr_families);
202  static bool Decode(LLGR *llgr_params,
203  const std::vector<Capability *> &capabilities);
204  static void GetFamilies(const LLGR &llgr_params,
205  std::vector<std::string> *families);
206  uint32_t time;
207  std::vector<Family> families;
208  };
209 
210  int code;
211  std::vector<uint8_t> capability;
212  };
213 
214  struct OptParam : public ParseObject {
217  }
218  std::vector<Capability *> capabilities;
219  };
220  std::vector<OptParam *> opt_params;
221  static BgpProto::OpenMessage *Decode(const uint8_t *data, size_t size);
222  static int EncodeData(OpenMessage *msg, uint8_t *data, size_t size);
223  virtual const std::string ToString() const;
224 
225  private:
226  int ValidateCapabilities(BgpPeer *peer) const;
227  int EncodeCapabilities(OpenMessage *msg, uint8_t *data, size_t size);
228  };
229 
230  struct Notification : public BgpMessage {
231  enum Code {
236  FSMErr = 5,
237  Cease = 6
238  };
239  static std::string CodeToString(Code code) {
240  switch (code) {
241  case MsgHdrErr:
242  return "Message Header Error";
243  case OpenMsgErr:
244  return "OPEN Message Error";
245  case UpdateMsgErr:
246  return "UPDATE Message Error";
247  case HoldTimerExp:
248  return "Hold Timer Expired";
249  case FSMErr:
250  return "Finite State Machine Error";
251  case Cease:
252  return "Cease";
253  }
254 
255  std::ostringstream oss;
256  oss << "Unknown(" << code << ")";
257  return oss.str();
258  }
263  };
264  static std::string MsgHdrSubcodeToString(MsgHdrSubCode sub_code) {
265  switch (sub_code) {
266  case ConnNotSync:
267  return "Connection Not Synchronized";
268  case BadMsgLength:
269  return "Bad Message Length";
270  case BadMsgType:
271  return "Bad Message Type";
272  }
273 
274  std::ostringstream oss;
275  oss << "Unknown(" << sub_code << ")";
276  return oss.str();
277  }
281  BadBgpId = 3,
286  };
287  static std::string OpenMsgSubcodeToString(OpenMsgSubCode sub_code) {
288  switch (sub_code) {
289  case UnsupportedVersion:
290  return "Unsupported Version Number";
291  case BadPeerAS:
292  return "Bad Peer AS";
293  case BadBgpId:
294  return "Bad BGP Identifier";
296  return "Unsupported Optional Parameter";
298  return "Authentication Failure";
300  return "Unacceptable Hold Time";
302  return "Unsupported Capability";
303  }
304 
305  std::ostringstream oss;
306  oss << "Unknown(" << sub_code << ")";
307  return oss.str();
308  }
319  MalformedASPath = 11
320  };
321  static std::string UpdateMsgSubCodeToString(UpdateMsgSubCode sub_code) {
322  switch (sub_code) {
324  return "Malformed Attribute List";
326  return "Unrecognized Well-known Attribute";
328  return "Missing Well-known Attribute";
329  case AttribFlagsError:
330  return "Attribute Flags Error";
331  case AttribLengthError:
332  return "Attribute Length Error";
333  case InvalidOrigin:
334  return "Invalid ORIGIN Attribute";
335  case InvalidNH:
336  return "Invalid NEXT_HOP Attribute";
337  case OptionalAttribError:
338  return "Optional Attribute Error";
339  case InvalidNetworkField:
340  return "Invalid Network Field";
341  case MalformedASPath:
342  return "Malformed AS_PATH";
343  }
344 
345  std::ostringstream oss;
346  oss << "Unknown(" << sub_code << ")";
347  return oss.str();
348  }
349  enum FsmSubcode {
353  EstablishedError = 3
354  };
355  static std::string FsmSubcodeToString(FsmSubcode sub_code) {
356  switch (sub_code) {
357  case UnspecifiedError:
358  return "Unspecified Error";
359  case OpenSentError:
360  return "Receive Unexpected Message in OpenSent State";
361  case OpenConfirmError:
362  return "Receive Unexpected Message in OpenConfirm State";
363  case EstablishedError:
364  return "Receive Unexpected Message in Established State";
365  }
366 
367  std::ostringstream oss;
368  oss << "Unknown(" << sub_code << ")";
369  return oss.str();
370  }
372  Unknown = 0,
382  };
383 
384  static std::string CeaseSubcodeToString(CeaseSubCode sub_code) {
385  switch (sub_code) {
386  case Unknown:
387  return "Unspecified Error";
388  case MaxPrefixes:
389  return "Received Maximum prefixes from peer";
390  case AdminShutdown:
391  return "Administrator has disabled the peer";
392  case PeerDeconfigured:
393  return "Administrator has unconfigured the peer";
394  case AdminReset:
395  return "Administrator reset the peer";
396  case ConnectionRejected:
397  return "Connection is rejected by the peer";
398  case OtherConfigChange:
399  return "Peer configuration has changed";
400  case ConnectionCollision:
401  return "Connection collision";
402  case OutOfResources:
403  return "Unable handle peer due to resource limit";
404  case HardReset:
405  return "Received HardReset to skip GR Helper mode";
406  }
407 
408  std::ostringstream oss;
409  oss << "Unknown(" << sub_code << ")";
410  return oss.str();
411  }
412 
413  Notification();
414  int error;
415  int subcode;
416  std::string data;
417  static BgpProto::Notification *Decode(const uint8_t *data, size_t size);
418  static int EncodeData(Notification *msg, uint8_t *data, size_t size);
419  virtual const std::string ToString() const;
420  static const std::string toString(Code code, int subcode);
421  };
422 
423  struct Keepalive : public BgpMessage {
424  Keepalive();
425  static BgpProto::Keepalive *Decode(const uint8_t *data, size_t size);
426  };
427 
428  struct Update : public BgpMessage {
429  Update();
430  ~Update();
431  int Validate(const BgpPeer *, std::string *data);
432  int CompareTo(const Update &rhs) const;
433  static BgpProto::Update *Decode(const uint8_t *data, size_t size);
434 
435  std::vector <BgpProtoPrefix *> withdrawn_routes;
436  std::vector <BgpAttribute *> path_attributes;
437  std::vector <BgpProtoPrefix *> nlri;
438  static int EncodeData(Update *msg, uint8_t *data, size_t size);
439  };
440 
441  static const int kMinMessageSize = 19;
442  static const int kMaxMessageSize = 4096;
443 
444  static BgpMessage *Decode(const uint8_t *data, size_t size,
445  ParseErrorContext *ec = NULL, bool as4 = false);
446 
447  static int Encode(const BgpMessage *msg, uint8_t *data, size_t size,
448  EncodeOffsets *offsets = NULL, bool as4 = false);
449  static int Encode(const BgpMpNlri *msg, uint8_t *data, size_t size,
450  EncodeOffsets *offsets = NULL);
451 };
452 
453 #endif // SRC_BGP_BGP_PROTO_H_
@ NOTIFICATION
Definition: bgp_proto.h:25
@ UPDATE
Definition: bgp_proto.h:24
@ KEEPALIVE
Definition: bgp_proto.h:26
static const int kMinMessageSize
Definition: bgp_proto.h:441
static const int kMaxMessageSize
Definition: bgp_proto.h:442
static BgpMessage * Decode(const uint8_t *data, size_t size, ParseErrorContext *ec=NULL, bool as4=false)
Definition: bgp_proto.cc:2116
static std::string BgpPeerTypeString(BgpPeerType peer_type)
Definition: bgp_proto.h:35
static int Encode(const BgpMessage *msg, uint8_t *data, size_t size, EncodeOffsets *offsets=NULL, bool as4=false)
Definition: bgp_proto.cc:2136
virtual const std::string ToString() const
Definition: bgp_proto.h:55
MessageType type
Definition: bgp_proto.h:54
BgpMessage(MessageType type)
Definition: bgp_proto.h:52
static BgpProto::Keepalive * Decode(const uint8_t *data, size_t size)
virtual const std::string ToString() const
Definition: bgp_proto.cc:344
static std::string CodeToString(Code code)
Definition: bgp_proto.h:239
static std::string FsmSubcodeToString(FsmSubcode sub_code)
Definition: bgp_proto.h:355
static std::string OpenMsgSubcodeToString(OpenMsgSubCode sub_code)
Definition: bgp_proto.h:287
static int EncodeData(Notification *msg, uint8_t *data, size_t size)
static const std::string toString(Code code, int subcode)
Definition: bgp_proto.cc:348
static std::string CeaseSubcodeToString(CeaseSubCode sub_code)
Definition: bgp_proto.h:384
static BgpProto::Notification * Decode(const uint8_t *data, size_t size)
static std::string MsgHdrSubcodeToString(MsgHdrSubCode sub_code)
Definition: bgp_proto.h:264
static std::string UpdateMsgSubCodeToString(UpdateMsgSubCode sub_code)
Definition: bgp_proto.h:321
Family(uint16_t afi, uint8_t safi, uint8_t flags)
Definition: bgp_proto.h:141
static Capability * Encode(uint16_t gr_time, bool restarted, bool notification, const std::vector< uint8_t > &gr_afi_flags, const std::vector< Address::Family > &gr_families)
Definition: bgp_proto.cc:130
std::vector< Family > families
Definition: bgp_proto.h:171
void set_flags(uint16_t gr_cap_bytes)
Definition: bgp_proto.h:163
void set_time(uint16_t gr_cap_bytes)
Definition: bgp_proto.h:166
static void GetFamilies(const GR &gr_params, std::vector< std::string > *families)
Definition: bgp_proto.cc:157
static bool Decode(GR *gr_params, const std::vector< Capability * > &capabilities)
Definition: bgp_proto.cc:176
Family(uint16_t afi, uint8_t safi, uint8_t flags, uint32_t time)
Definition: bgp_proto.h:185
static void GetFamilies(const LLGR &llgr_params, std::vector< std::string > *families)
Definition: bgp_proto.cc:261
static Capability * Encode(uint32_t llgr_time, uint8_t llgr_afi_flags, const std::vector< Address::Family > &llgr_families)
Definition: bgp_proto.cc:208
static bool Decode(LLGR *llgr_params, const std::vector< Capability * > &capabilities)
Definition: bgp_proto.cc:228
static std::string CapabilityToString(int capability)
Definition: bgp_proto.h:87
std::vector< uint8_t > capability
Definition: bgp_proto.h:211
Capability(int code, const uint8_t *src, int size)
Definition: bgp_proto.h:124
std::vector< Capability * > capabilities
Definition: bgp_proto.h:218
int ValidateCapabilities(BgpPeer *peer) const
Definition: bgp_proto.cc:51
static BgpProto::OpenMessage * Decode(const uint8_t *data, size_t size)
int EncodeCapabilities(OpenMessage *msg, uint8_t *data, size_t size)
static int EncodeData(OpenMessage *msg, uint8_t *data, size_t size)
int Validate(BgpPeer *peer) const
Definition: bgp_proto.cc:107
virtual const std::string ToString() const
Definition: bgp_proto.cc:281
std::vector< OptParam * > opt_params
Definition: bgp_proto.h:220
std::vector< BgpAttribute * > path_attributes
Definition: bgp_proto.h:436
static int EncodeData(Update *msg, uint8_t *data, size_t size)
std::vector< BgpProtoPrefix * > withdrawn_routes
Definition: bgp_proto.h:435
std::vector< BgpProtoPrefix * > nlri
Definition: bgp_proto.h:437
int Validate(const BgpPeer *, std::string *data)
Definition: bgp_proto.cc:408
static BgpProto::Update * Decode(const uint8_t *data, size_t size)
int CompareTo(const Update &rhs) const
Definition: bgp_proto.cc:491
void STLDeleteValues(Container *container)
Definition: util.h:102