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