libbgp  0.6
A C++ BGP Library.
bgp-filter.cc
Go to the documentation of this file.
1 
11 #include <arpa/inet.h>
12 #include "bgp-filter.h"
13 #include "value-op.h"
14 
15 namespace libbgp {
16 
17 BgpFilterRule::~BgpFilterRule() {}
18 
27  this->op = op;
28  this->match_type = type;
29  this->prefix = prefix;
30 }
31 
40  this->op = op;
41  this->match_type = type;
42  this->prefix = prefix;
43 }
44 
53  this->op = op;
54  this->match_type = type;
55  this->asn = asn;
56 }
57 
58 BgpFilterOP BgpFilterRuleAsPath::apply(__attribute__((unused)) const Prefix &prefix, const std::vector<std::shared_ptr<BgpPathAttrib>> &attribs) {
59  for (const std::shared_ptr<BgpPathAttrib> &attr : attribs) {
60  if (attr->type_code != AS_PATH) continue;
61  const BgpPathAttribAsPath &as_path = dynamic_cast<const BgpPathAttribAsPath &>(*attr);
62 
63  for (const BgpAsPathSegment &as_seg : as_path.as_paths) {
64  if (as_seg.type != AS_SEQUENCE) continue;
65 
66  if (match_type == M_FROM_ASN && asn == as_seg.value.back()) return op;
67  if (match_type == M_NOT_FROM_ASN && asn != as_seg.value.back()) return op;
68 
69  for (uint32_t asn : as_seg.value) {
70  if (asn == this->asn && match_type == M_HAS_ASN) return op;
71  }
72 
73  }
74 
75  if (match_type == M_NOT_HAS_ASN) return op;
76  }
77 
78  return NOP;
79 }
80 
90  this->op = op;
91  this->match_type = type;
92  uint8_t *community_ptr = (uint8_t *) &community;
93  putValue<uint16_t>(&community_ptr, htons(asn));
94  putValue<uint16_t>(&community_ptr, htons(keyword));
95 }
96 
105  this->op = op;
106  this->match_type = type;
107  this->community = htonl(community);
108 }
109 
110 BgpFilterOP BgpFilterRuleCommunity::apply(__attribute__((unused)) const Prefix &prefix, const std::vector<std::shared_ptr<BgpPathAttrib>> &attribs) {
111  for (const std::shared_ptr<BgpPathAttrib> &attr : attribs) {
112  if (attr->type_code != COMMUNITY) continue;
113  const BgpPathAttribCommunity &community = dynamic_cast<const BgpPathAttribCommunity &>(*attr);
114 
115  for (uint32_t community_val : community.communites) {
116  if (match_type == M_HAS_COMMUNITY && community_val == this->community) return op;
117  }
118 
119  if (match_type == M_NOT_HAS_COMMUNITY) return op;
120  }
121 
122  return NOP;
123 }
124 
132  default_op = ACCEPT;
133 }
134 
141  this->default_op = default_op;
142 }
143 
151 BgpFilterOP BgpFilterRules::apply(const Prefix &prefix, const std::vector<std::shared_ptr<BgpPathAttrib>> &attribs) {
152  if (rules.size() == 0) return default_op;
153 
154  auto rule = rules.end();
155 
156  do {
157  rule--;
158  BgpFilterOP this_op = (*rule)->apply(prefix, attribs);
159  if (this_op != NOP) return this_op;
160  } while (rule != rules.begin());
161 
162  return default_op;
163 }
164 
165 }
IPv6 Route/Prefix related utilities.
Definition: prefix6.h:27
std::vector< uint32_t > value
The segment value.
BgpFilterOP
The filter operation.
Definition: bgp-filter.h:69
BgpFilterRuleRouteMatchType
Matching type of IP prefix rule.
Definition: bgp-filter.h:36
std::vector< uint32_t > communites
Raw community attribute in network byte order.
BgpFilterOP apply(const Prefix &prefix, const std::vector< std::shared_ptr< BgpPathAttrib >> &attribs)
Apply the rules set on a route.
Definition: bgp-filter.cc:151
BgpFilterRules()
Construct a new BgpFilterRules rules set.
Definition: bgp-filter.cc:131
BgpFilterRuleCommunityMatchType
Matching type of COMMUNITY rule.
Definition: bgp-filter.h:60
std::vector< BgpAsPathSegment > as_paths
The AS Path segments.
BgpFilterRuleRoute6(BgpFilterOP op, BgpFilterRuleRouteMatchType type, const Prefix6 &prefix)
Construct a new IPv6 route filtering object.
Definition: bgp-filter.cc:39
uint8_t type
Segment type.
BgpFilterRuleCommunity(BgpFilterOP op, BgpFilterRuleCommunityMatchType type, uint16_t asn, uint16_t keyword)
Construct a new COMMUNITY filtering object.
Definition: bgp-filter.cc:89
BgpFilterRuleRoute4(BgpFilterOP op, BgpFilterRuleRouteMatchType type, const Prefix4 &prefix)
Construct a new IPv4 route filtering object.
Definition: bgp-filter.cc:26
IPv4 Route/Prefix related utilities.
Definition: prefix4.h:25
Buffer operation helpers.
Route filtering engine.
Definition: bgp-afi.h:14
Route/Prefix related utilities.
Definition: prefix.h:25
An AS_PATH or AS4_PATH segment.
BgpFilterRuleAsPathMatchType
Matching type of AS_PATH rule.
Definition: bgp-filter.h:49
BgpFilterOP apply(const Prefix &prefix, const std::vector< std::shared_ptr< BgpPathAttrib >> &attribs)
Apply the rule on a route.
Definition: bgp-filter.cc:110
BgpFilterOP apply(const Prefix &prefix, const std::vector< std::shared_ptr< BgpPathAttrib >> &attribs)
Apply the rule on a route.
Definition: bgp-filter.cc:58
BGP community attribute.
BgpFilterRuleAsPath(BgpFilterOP op, BgpFilterRuleAsPathMatchType type, uint32_t asn)
Construct a new AS_PATH filtering object.
Definition: bgp-filter.cc:52