PcapPlusPlus  Next
BgpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include "Layer.h"
5 #include "IpAddress.h"
6 
12 
15 namespace pcpp
16 {
17 
21  class BgpLayer : public Layer
22  {
23  public:
26  {
28  Open = 1,
30  Update = 2,
34  Keepalive = 4,
37  };
38 
39 #pragma pack(push, 1)
43  {
45  uint8_t marker[16];
47  uint16_t length;
49  uint8_t messageType;
50  };
51 #pragma pack(pop)
52  static_assert(sizeof(bgp_common_header) == 19, "bgp_common_header size is not 19 bytes");
53 
55  virtual BgpMessageType getBgpMessageType() const = 0;
56 
59  std::string getMessageTypeAsString() const;
60 
65  static bool isBgpPort(uint16_t portSrc, uint16_t portDst)
66  {
67  return portSrc == 179 || portDst == 179;
68  }
69 
78  static BgpLayer* parseBgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
79 
80  // implement abstract methods
81 
83  size_t getHeaderLen() const override;
84 
87  void parseNextLayer() override;
88 
89  std::string toString() const override;
90 
92  {
94  }
95 
100  void computeCalculateFields() override;
101 
102  protected:
103  // protected c'tors, this class cannot be instantiated by users
104  BgpLayer()
105  {}
106  BgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
107  : Layer(data, dataLen, prevLayer, packet, BGP)
108  {}
109 
110  bgp_common_header* getBasicHeader() const
111  {
112  return reinterpret_cast<bgp_common_header*>(m_Data);
113  }
114 
115  void setBgpFields(size_t messageLen = 0);
116 
117  bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend) override;
118 
119  bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten) override;
120  };
121 
125  {
126  public:
127 #pragma pack(push, 1)
131  {
133  uint8_t version;
137  uint16_t holdTime;
139  uint32_t bgpId;
143 #pragma pack(pop)
144 
148  {
150  uint8_t type = 0;
152  uint8_t length = 0;
154  uint8_t value[32] = {};
155 
157  optional_parameter() = default;
158 
163  optional_parameter(uint8_t typeVal, const std::string& valueAsHexString);
164  };
165 
171  BgpOpenMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
172  : BgpLayer(data, dataLen, prevLayer, packet)
173  {}
174 
179  static bool isDataValid(const uint8_t* data, size_t dataSize);
180 
187  BgpOpenMessageLayer(uint16_t myAutonomousSystem, uint16_t holdTime, const IPv4Address& bgpId,
188  const std::vector<optional_parameter>& optionalParams = std::vector<optional_parameter>());
189 
194  {
195  return reinterpret_cast<bgp_open_message*>(m_Data);
196  }
197 
200  {
201  return IPv4Address(getOpenMsgHeader()->bgpId);
202  }
203 
206  void setBgpId(const IPv4Address& newBgpId);
207 
211  void getOptionalParameters(std::vector<optional_parameter>& optionalParameters);
212 
215 
223  bool setOptionalParameters(const std::vector<optional_parameter>& optionalParameters);
224 
230 
231  // implement abstract methods
232 
234  {
235  return BgpLayer::Open;
236  }
237 
238  private:
239  size_t optionalParamsToByteArray(const std::vector<optional_parameter>& optionalParams, uint8_t* resultByteArr,
240  size_t maxByteArrSize);
241  };
242 
246  {
247  public:
252  {
254  uint8_t prefix;
257 
260  {}
261 
265  prefix_and_ip(uint8_t prefixVal, const std::string& ipAddrVal) : prefix(prefixVal), ipAddr(ipAddrVal)
266  {}
267  };
268 
272  {
274  uint8_t flags;
276  uint8_t type;
278  uint8_t length;
280  uint8_t data[32];
281 
282  // FIXME: This does not actually zero the data.
285  {}
286 
293  path_attribute(uint8_t flagsVal, uint8_t typeVal, const std::string& dataAsHexString);
294  };
295 
301  BgpUpdateMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
302  : BgpLayer(data, dataLen, prevLayer, packet)
303  {}
304 
309  static bool isDataValid(const uint8_t* data, size_t dataSize);
310 
319  const std::vector<prefix_and_ip>& withdrawnRoutes = std::vector<prefix_and_ip>(),
320  const std::vector<path_attribute>& pathAttributes = std::vector<path_attribute>(),
321  const std::vector<prefix_and_ip>& nlri = std::vector<prefix_and_ip>());
322 
327  {
328  return reinterpret_cast<bgp_common_header*>(m_Data);
329  }
330 
332  size_t getWithdrawnRoutesLength() const;
333 
336  void getWithdrawnRoutes(std::vector<prefix_and_ip>& withdrawnRoutes);
337 
344  bool setWithdrawnRoutes(const std::vector<prefix_and_ip>& withdrawnRoutes);
345 
351 
353  size_t getPathAttributesLength() const;
354 
357  void getPathAttributes(std::vector<path_attribute>& pathAttributes);
358 
365  bool setPathAttributes(const std::vector<path_attribute>& pathAttributes);
366 
372 
375 
378  void getNetworkLayerReachabilityInfo(std::vector<prefix_and_ip>& nlri);
379 
386  bool setNetworkLayerReachabilityInfo(const std::vector<prefix_and_ip>& nlri);
387 
393 
394  // implement abstract methods
395 
397  {
398  return BgpLayer::Update;
399  }
400 
401  private:
402  void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector<prefix_and_ip>& result);
403 
404  size_t prefixAndIPDataToByteArray(const std::vector<prefix_and_ip>& prefixAndIpData, uint8_t* resultByteArr,
405  size_t maxByteArrSize);
406 
407  size_t pathAttributesToByteArray(const std::vector<path_attribute>& pathAttributes, uint8_t* resultByteArr,
408  size_t maxByteArrSize);
409  };
410 
414  {
415  public:
416 #pragma pack(push, 1)
420  {
422  uint8_t errorCode;
424  uint8_t errorSubCode;
426 #pragma pack(pop)
427 
433  BgpNotificationMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
434  : BgpLayer(data, dataLen, prevLayer, packet)
435  {}
436 
440  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode);
441 
447  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData,
448  size_t notificationDataLen);
449 
456  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const std::string& notificationData);
457 
462  {
463  return reinterpret_cast<bgp_notification_message*>(m_Data);
464  }
465 
468  size_t getNotificationDataLen() const;
469 
472  uint8_t* getNotificationData() const;
473 
476  std::string getNotificationDataAsHexString() const;
477 
485  bool setNotificationData(const uint8_t* newNotificationData, size_t newNotificationDataLen);
486 
494  bool setNotificationData(const std::string& newNotificationDataAsHexString);
495 
496  // implement abstract methods
497 
499  {
500  return BgpLayer::Notification;
501  }
502 
503  private:
504  void initMessageData(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData,
505  size_t notificationDataLen);
506  };
507 
511  {
512  public:
516 
522  BgpKeepaliveMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
523  : BgpLayer(data, dataLen, prevLayer, packet)
524  {}
525 
528 
533  {
534  return reinterpret_cast<bgp_keepalive_message*>(getBasicHeader());
535  }
536 
537  // implement abstract methods
538 
540  {
541  return BgpLayer::Keepalive;
542  }
543  };
544 
548  {
549  public:
550 #pragma pack(push, 1)
554  {
556  uint16_t afi;
558  uint8_t reserved;
560  uint8_t safi;
562 #pragma pack(pop)
563 
569  BgpRouteRefreshMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
570  : BgpLayer(data, dataLen, prevLayer, packet)
571  {}
572 
576  BgpRouteRefreshMessageLayer(uint16_t afi, uint8_t safi);
577 
582  {
583  return reinterpret_cast<bgp_route_refresh_message*>(getBasicHeader());
584  }
585 
586  // implement abstract methods
587 
589  {
590  return BgpLayer::RouteRefresh;
591  }
592  };
593 
594 } // namespace pcpp
Definition: BgpLayer.h:511
bgp_common_header bgp_keepalive_message
Definition: BgpLayer.h:515
BgpMessageType getBgpMessageType() const override
Definition: BgpLayer.h:539
BgpKeepaliveMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:522
bgp_keepalive_message * getKeepaliveHeader() const
Definition: BgpLayer.h:532
BgpKeepaliveMessageLayer()
A c'tor that creates a new BGP KEEPALIVE message.
Definition: BgpLayer.h:22
void computeCalculateFields() override
static bool isBgpPort(uint16_t portSrc, uint16_t portDst)
Definition: BgpLayer.h:65
BgpMessageType
An enum representing BGP message types.
Definition: BgpLayer.h:26
@ RouteRefresh
BGP ROUTE-REFRESH message.
Definition: BgpLayer.h:36
@ Update
BGP UPDATE message.
Definition: BgpLayer.h:30
@ Keepalive
BGP KEEPALIVE message.
Definition: BgpLayer.h:34
@ Notification
BGP NOTIFICATION message.
Definition: BgpLayer.h:32
@ Open
BGP OPEN message.
Definition: BgpLayer.h:28
std::string toString() const override
std::string getMessageTypeAsString() const
OsiModelLayer getOsiModelLayer() const override
Definition: BgpLayer.h:91
size_t getHeaderLen() const override
void parseNextLayer() override
static BgpLayer * parseBgpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
virtual BgpMessageType getBgpMessageType() const =0
Definition: BgpLayer.h:414
BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode)
BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const std::string &notificationData)
BgpMessageType getBgpMessageType() const override
Definition: BgpLayer.h:498
bool setNotificationData(const std::string &newNotificationDataAsHexString)
bgp_notification_message * getNotificationMsgHeader() const
Definition: BgpLayer.h:461
std::string getNotificationDataAsHexString() const
bool setNotificationData(const uint8_t *newNotificationData, size_t newNotificationDataLen)
BgpNotificationMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:433
BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const uint8_t *notificationData, size_t notificationDataLen)
uint8_t * getNotificationData() const
Definition: BgpLayer.h:125
void getOptionalParameters(std::vector< optional_parameter > &optionalParameters)
BgpMessageType getBgpMessageType() const override
Definition: BgpLayer.h:233
static bool isDataValid(const uint8_t *data, size_t dataSize)
BgpOpenMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:171
void setBgpId(const IPv4Address &newBgpId)
bool setOptionalParameters(const std::vector< optional_parameter > &optionalParameters)
BgpOpenMessageLayer(uint16_t myAutonomousSystem, uint16_t holdTime, const IPv4Address &bgpId, const std::vector< optional_parameter > &optionalParams=std::vector< optional_parameter >())
bgp_open_message * getOpenMsgHeader() const
Definition: BgpLayer.h:193
IPv4Address getBgpId() const
Definition: BgpLayer.h:199
size_t getOptionalParametersLength()
Definition: BgpLayer.h:548
BgpRouteRefreshMessageLayer(uint16_t afi, uint8_t safi)
BgpMessageType getBgpMessageType() const override
Definition: BgpLayer.h:588
BgpRouteRefreshMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:569
bgp_route_refresh_message * getRouteRefreshHeader() const
Definition: BgpLayer.h:581
Definition: BgpLayer.h:246
void getPathAttributes(std::vector< path_attribute > &pathAttributes)
BgpMessageType getBgpMessageType() const override
Definition: BgpLayer.h:396
BgpUpdateMessageLayer(const std::vector< prefix_and_ip > &withdrawnRoutes=std::vector< prefix_and_ip >(), const std::vector< path_attribute > &pathAttributes=std::vector< path_attribute >(), const std::vector< prefix_and_ip > &nlri=std::vector< prefix_and_ip >())
static bool isDataValid(const uint8_t *data, size_t dataSize)
void getWithdrawnRoutes(std::vector< prefix_and_ip > &withdrawnRoutes)
bool setPathAttributes(const std::vector< path_attribute > &pathAttributes)
bool setNetworkLayerReachabilityInfo(const std::vector< prefix_and_ip > &nlri)
size_t getNetworkLayerReachabilityInfoLength() const
bool setWithdrawnRoutes(const std::vector< prefix_and_ip > &withdrawnRoutes)
size_t getPathAttributesLength() const
size_t getWithdrawnRoutesLength() const
bgp_common_header * getBasicMsgHeader() const
Definition: BgpLayer.h:326
void getNetworkLayerReachabilityInfo(std::vector< prefix_and_ip > &nlri)
BgpUpdateMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:301
Definition: IpAddress.h:30
Definition: Layer.h:116
Definition: Packet.h:48
const ProtocolType BGP
Border Gateway Protocol (BGP) version 4 protocol.
Definition: ProtocolType.h:165
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:267
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:281
Definition: BgpLayer.h:43
uint8_t messageType
BGP message type.
Definition: BgpLayer.h:49
uint8_t marker[16]
16-octet marker
Definition: BgpLayer.h:45
uint16_t length
Total length of the message, including the header.
Definition: BgpLayer.h:47
uint8_t errorSubCode
BGP notification error sub-code.
Definition: BgpLayer.h:424
uint8_t errorCode
BGP notification error code.
Definition: BgpLayer.h:422
uint16_t holdTime
The number of seconds the sender proposes for the value of the Hold Timer.
Definition: BgpLayer.h:137
uint8_t version
BGP version number.
Definition: BgpLayer.h:133
uint32_t bgpId
BGP Identifier of the sender.
Definition: BgpLayer.h:139
uint16_t myAutonomousSystem
Autonomous System number of the sender.
Definition: BgpLayer.h:135
uint8_t optionalParameterLength
The total length of the Optional Parameters field.
Definition: BgpLayer.h:141
optional_parameter(uint8_t typeVal, const std::string &valueAsHexString)
uint8_t type
Parameter type.
Definition: BgpLayer.h:150
optional_parameter()=default
A default c'tor that zeroes all data.
uint8_t length
Parameter length.
Definition: BgpLayer.h:152
uint8_t value[32]
Parameter data.
Definition: BgpLayer.h:154
uint8_t safi
Subsequent Address Family Identifier.
Definition: BgpLayer.h:560
uint16_t afi
Address Family Identifier.
Definition: BgpLayer.h:556
uint8_t reserved
Reserved field.
Definition: BgpLayer.h:558
path_attribute()
A default c'tor that zeroes all data.
Definition: BgpLayer.h:284
uint8_t type
Path attribute type.
Definition: BgpLayer.h:276
uint8_t flags
Path attribute flags.
Definition: BgpLayer.h:274
uint8_t data[32]
Path attribute data. Max supported data length is 32 bytes.
Definition: BgpLayer.h:280
path_attribute(uint8_t flagsVal, uint8_t typeVal, const std::string &dataAsHexString)
uint8_t length
Path attribute length.
Definition: BgpLayer.h:278
uint8_t prefix
IPv4 address mask, must contain one of the values: 8, 16, 24, 32.
Definition: BgpLayer.h:254
prefix_and_ip()
A default c'tor that zeroes all data.
Definition: BgpLayer.h:259
prefix_and_ip(uint8_t prefixVal, const std::string &ipAddrVal)
Definition: BgpLayer.h:265
IPv4Address ipAddr
IPv4 address.
Definition: BgpLayer.h:256