Line data Source code
1 : /*
2 : * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "bgp/routing-policy/routing_policy_match.h"
7 :
8 : #include <boost/foreach.hpp>
9 : #include <boost/assign/list_of.hpp>
10 :
11 : #include <algorithm>
12 : #include <map>
13 : #include <sstream>
14 : #include <vector>
15 :
16 : #include "bgp/ipeer.h"
17 : #include "bgp/bgp_attr.h"
18 : #include "bgp/bgp_path.h"
19 : #include "bgp/extended-community/default_gateway.h"
20 : #include "bgp/extended-community/es_import.h"
21 : #include "bgp/extended-community/esi_label.h"
22 : #include "bgp/extended-community/etree.h"
23 : #include "bgp/extended-community/load_balance.h"
24 : #include "bgp/extended-community/mac_mobility.h"
25 : #include "bgp/extended-community/router_mac.h"
26 : #include "bgp/extended-community/site_of_origin.h"
27 : #include "bgp/extended-community/source_as.h"
28 : #include "bgp/extended-community/tag.h"
29 : #include "bgp/extended-community/vrf_route_import.h"
30 : #include "bgp/origin-vn/origin_vn.h"
31 : #include "bgp/routing-instance/routepath_replicator.h"
32 : #include "bgp/routing-instance/routing_instance.h"
33 : #include "bgp/rtarget/rtarget_address.h"
34 : #include "bgp/security_group/security_group.h"
35 : #include "bgp/tunnel_encap/tunnel_encap.h"
36 : #include "net/community_type.h"
37 :
38 :
39 : using contrail::regex;
40 : using contrail::regex_match;
41 : using contrail::regex_search;
42 : using std::includes;
43 : using std::map;
44 : using std::ostringstream;
45 : using std::sort;
46 : using std::string;
47 : using std::unique;
48 : using std::vector;
49 : using std::find;
50 :
51 0 : MatchCommunity::MatchCommunity(const vector<string> &communities,
52 0 : bool match_all) : match_all_(match_all) {
53 : // Assume that the each community string that doesn't correspond to a
54 : // community name or value is a regex string.
55 0 : BOOST_FOREACH(const string &community, communities) {
56 0 : uint32_t value = CommunityType::CommunityFromString(community);
57 0 : if (value) {
58 0 : to_match_.insert(value);
59 : } else {
60 0 : to_match_regex_strings_.push_back(community);
61 : }
62 : }
63 :
64 : // Sort and uniquify the vector of regex strings.
65 0 : sort(to_match_regex_strings_.begin(), to_match_regex_strings_.end());
66 : vector<string>::iterator it =
67 0 : unique(to_match_regex_strings_.begin(), to_match_regex_strings_.end());
68 0 : to_match_regex_strings_.erase(it, to_match_regex_strings_.end());
69 :
70 : // Build a vector of regexs corresponding to the regex strings.
71 0 : BOOST_FOREACH(string regex_str, regex_strings()) {
72 0 : to_match_regexs_.push_back(regex(regex_str));
73 0 : }
74 0 : }
75 :
76 0 : MatchCommunity::~MatchCommunity() {
77 0 : }
78 :
79 : //
80 : // Return true if all community strings (normal or regex) are matched by the
81 : // community values in the BgpAttr.
82 : //
83 0 : bool MatchCommunity::MatchAll(const BgpAttr *attr) const {
84 : // Bail if there's no community values in the BgpAttr.
85 0 : const Community *comm = attr->community();
86 0 : if (!comm)
87 0 : return false;
88 :
89 : // Make sure that all non-regex communities in this MatchCommunity are
90 : // present in the BgpAttr.
91 0 : vector<uint32_t> list = comm->communities();
92 0 : sort(list.begin(), list.end());
93 0 : if (!includes(list.begin(), list.end(),
94 0 : communities().begin(), communities().end())) {
95 0 : return false;
96 : }
97 :
98 : // Make sure that each regex in this MatchCommunity is matched by one
99 : // of the communities in the BgpAttr.
100 0 : BOOST_FOREACH(const regex &match_expr, regexs()) {
101 0 : bool matched = false;
102 0 : BOOST_FOREACH(uint32_t community, comm->communities()) {
103 0 : string community_str = CommunityType::CommunityToString(community);
104 0 : if (regex_match(community_str, match_expr)) {
105 0 : matched = true;
106 0 : break;
107 : }
108 0 : }
109 0 : if (!matched)
110 0 : return false;
111 : }
112 :
113 0 : return true;
114 0 : }
115 :
116 : //
117 : // Return true if any community strings (normal or regex) is matched by the
118 : // community values in the BgpAttr.
119 : //
120 0 : bool MatchCommunity::MatchAny(const BgpAttr *attr) const {
121 : // Bail if there's no community values in the BgpAttr.
122 0 : const Community *comm = attr->community();
123 0 : if (!comm)
124 0 : return false;
125 :
126 : // Check if any of the community values in the BgpAttr matches one of
127 : // the normal community strings.
128 0 : BOOST_FOREACH(uint32_t community, comm->communities()) {
129 0 : if (communities().find(community) != communities().end())
130 0 : return true;
131 : }
132 :
133 : // Check if any of the community values in the BgpAttr matches one of
134 : // the community regexs.
135 0 : BOOST_FOREACH(uint32_t community, comm->communities()) {
136 0 : string community_str = CommunityType::CommunityToString(community);
137 0 : BOOST_FOREACH(const regex &match_expr, regexs()) {
138 0 : if (regex_match(community_str, match_expr))
139 0 : return true;
140 : }
141 0 : }
142 :
143 0 : return false;
144 : }
145 :
146 : //
147 : // Return true if the BgpPath matches this MatchCommunity.
148 : //
149 0 : bool MatchCommunity::Match(const BgpRoute *route, const BgpPath *path,
150 : const BgpAttr *attr) const {
151 0 : return (match_all_ ? MatchAll(attr) : MatchAny(attr));
152 : }
153 :
154 : //
155 : // Return string representation of this MatchCommunity.
156 : //
157 0 : string MatchCommunity::ToString() const {
158 0 : ostringstream oss;
159 0 : if (match_all_) {
160 0 : oss << "community (all) [ ";
161 : } else {
162 0 : oss << "community (any) [ ";
163 : }
164 0 : BOOST_FOREACH(uint32_t community, communities()) {
165 0 : string name = CommunityType::CommunityToString(community);
166 0 : oss << name << ",";
167 0 : }
168 0 : BOOST_FOREACH(string regex_str, regex_strings()) {
169 0 : oss << regex_str << ",";
170 0 : }
171 0 : oss.seekp(-1, oss.cur);
172 0 : oss << " ]";
173 0 : return oss.str();
174 0 : }
175 :
176 : //
177 : // Return true if this MatchCommunity is equal to the one supplied.
178 : //
179 0 : bool MatchCommunity::IsEqual(const RoutingPolicyMatch &community) const {
180 : const MatchCommunity in_community =
181 0 : static_cast<const MatchCommunity &>(community);
182 0 : if (match_all() != in_community.match_all())
183 0 : return false;
184 0 : if (communities() != in_community.communities())
185 0 : return false;
186 0 : if (regex_strings() != in_community.regex_strings())
187 0 : return false;
188 0 : return true;
189 0 : }
190 :
191 0 : MatchExtCommunity::MatchExtCommunity(const vector<string> &communities,
192 0 : bool match_all) : match_all_(match_all) {
193 : // Assume that the each community string that doesn't correspond to a
194 : // community name or value is a regex string.
195 0 : BOOST_FOREACH(const string &community, communities) {
196 : const ExtCommunity::ExtCommunityList list =
197 0 : ExtCommunity::ExtCommunityFromString(community);
198 0 : if (list.size()) {
199 0 : if(!Find(list[0]))
200 0 : to_match_.push_back(list[0]);
201 : } else {
202 0 : to_match_regex_strings_.push_back(community);
203 : }
204 0 : }
205 :
206 : // Sort and uniquify the vector of regex strings.
207 0 : sort(to_match_.begin(), to_match_.end());
208 0 : sort(to_match_regex_strings_.begin(), to_match_regex_strings_.end());
209 : vector<string>::iterator it =
210 0 : unique(to_match_regex_strings_.begin(), to_match_regex_strings_.end());
211 0 : to_match_regex_strings_.erase(it, to_match_regex_strings_.end());
212 :
213 : // Build a vector of regexs corresponding to the regex strings.
214 0 : BOOST_FOREACH(string regex_str, regex_strings()) {
215 0 : to_match_regexs_.push_back(regex(regex_str));
216 0 : }
217 0 : }
218 :
219 0 : MatchExtCommunity::~MatchExtCommunity() {
220 0 : }
221 :
222 : //
223 : // Return true if all community strings (normal or regex) are matched by the
224 : // community values in the BgpAttr.
225 : //
226 0 : bool MatchExtCommunity::MatchAll(const BgpAttr *attr) const {
227 : // Bail if there's no community values in the BgpAttr.
228 0 : const ExtCommunity *comm = attr->ext_community();
229 0 : if (!comm)
230 0 : return false;
231 :
232 : // Make sure that all non-regex communities in this MatchExtCommunity are
233 : // present in the BgpAttr.
234 0 : ExtCommunity::ExtCommunityList list = comm->communities();
235 0 : sort(list.begin(), list.end());
236 0 : if (!includes(list.begin(), list.end(),
237 0 : communities().begin(), communities().end())) {
238 0 : return false;
239 : }
240 :
241 : // Make sure that each regex in this MatchExtCommunity is matched by one
242 : // of the communities in the BgpAttr.
243 0 : BOOST_FOREACH(const regex &match_expr, regexs()) {
244 0 : bool matched = false;
245 0 : BOOST_FOREACH(ExtCommunity::ExtCommunityValue community,
246 : comm->communities()) {
247 0 : string community_str = ExtCommunity::ToString(community);
248 0 : if (regex_match(community_str, match_expr)) {
249 0 : matched = true;
250 0 : break;
251 : }
252 0 : community_str = ExtCommunity::ToHexString(community);
253 0 : if (regex_match(community_str, match_expr)) {
254 0 : matched = true;
255 0 : break;
256 : }
257 0 : }
258 0 : if (!matched)
259 0 : return false;
260 : }
261 :
262 0 : return true;
263 0 : }
264 :
265 0 : bool MatchExtCommunity::Find(const ExtCommunity::ExtCommunityValue &community)
266 : const {
267 0 : return (std::find(communities().begin(), communities().end(), community) !=
268 0 : communities().end());
269 : }
270 :
271 : //
272 : // Return true if any community strings (normal or regex) is matched by the
273 : // community values in the BgpAttr.
274 : //
275 0 : bool MatchExtCommunity::MatchAny(const BgpAttr *attr) const {
276 : // Bail if there's no community values in the BgpAttr.
277 0 : const ExtCommunity *comm = attr->ext_community();
278 0 : if (!comm)
279 0 : return false;
280 :
281 : // Check if any of the community values in the BgpAttr matches one of
282 : // the normal community strings.
283 0 : BOOST_FOREACH(ExtCommunity::ExtCommunityValue community,
284 : comm->communities()) {
285 0 : if (Find(community))
286 0 : return true;
287 : }
288 :
289 : // Check if any of the community values in the BgpAttr matches one of
290 : // the community regexs.
291 0 : BOOST_FOREACH(ExtCommunity::ExtCommunityValue community,
292 : comm->communities()) {
293 0 : string community_str = ExtCommunity::ToString(community);
294 0 : BOOST_FOREACH(const regex &match_expr, regexs()) {
295 0 : if (regex_match(community_str, match_expr))
296 0 : return true;
297 : }
298 0 : community_str = ExtCommunity::ToHexString(community);
299 0 : BOOST_FOREACH(const regex &match_expr, regexs()) {
300 0 : if (regex_match(community_str, match_expr))
301 0 : return true;
302 : }
303 0 : }
304 :
305 0 : return false;
306 : }
307 :
308 : //
309 : // Return true if the BgpPath matches this MatchExtCommunity.
310 : //
311 0 : bool MatchExtCommunity::Match(const BgpRoute *route, const BgpPath *path,
312 : const BgpAttr *attr) const {
313 0 : return (match_all_ ? MatchAll(attr) : MatchAny(attr));
314 : }
315 :
316 : //
317 : // Return string representation of this MatchExtCommunity.
318 : //
319 0 : string MatchExtCommunity::ToString() const {
320 0 : ostringstream oss;
321 0 : if (match_all_) {
322 0 : oss << "Extcommunity (all) [ ";
323 : } else {
324 0 : oss << "Extcommunity (any) [ ";
325 : }
326 0 : BOOST_FOREACH(ExtCommunity::ExtCommunityValue community, communities()) {
327 0 : string name = ExtCommunity::ToString(community);
328 0 : oss << name << ",";
329 0 : }
330 0 : BOOST_FOREACH(string regex_str, regex_strings()) {
331 0 : oss << regex_str << ",";
332 0 : }
333 0 : oss.seekp(-1, oss.cur);
334 0 : oss << " ]";
335 0 : return oss.str();
336 0 : }
337 :
338 : //
339 : // Return true if this MatchExtCommunity is equal to the one supplied.
340 : //
341 0 : bool MatchExtCommunity::IsEqual(const RoutingPolicyMatch &community) const {
342 : const MatchExtCommunity in_community =
343 0 : static_cast<const MatchExtCommunity &>(community);
344 0 : if (match_all() != in_community.match_all())
345 0 : return false;
346 0 : if (communities() != in_community.communities())
347 0 : return false;
348 0 : if (regex_strings() != in_community.regex_strings())
349 0 : return false;
350 0 : return true;
351 0 : }
352 :
353 : template <typename T>
354 0 : typename MatchPrefix<T>::MatchType MatchPrefix<T>::GetMatchType(
355 : const string &match_type_str) {
356 0 : MatchType match_type = EXACT;
357 0 : if (match_type_str == "exact") {
358 0 : match_type = EXACT;
359 0 : } else if (match_type_str == "longer") {
360 0 : match_type = LONGER;
361 0 : } else if (match_type_str == "orlonger") {
362 0 : match_type = ORLONGER;
363 : }
364 0 : return match_type;
365 : }
366 :
367 : template <typename T>
368 0 : MatchPrefix<T>::MatchPrefix(const PrefixMatchConfigList &match_config_list) {
369 0 : BOOST_FOREACH(const PrefixMatchConfig &match_config, match_config_list) {
370 0 : boost::system::error_code ec;
371 0 : PrefixT prefix = PrefixT::FromString(match_config.prefix_to_match, &ec);
372 : MatchType match_type =
373 0 : MatchPrefix<T>::GetMatchType(match_config.prefix_match_type);
374 0 : match_list_.push_back(PrefixMatch(prefix, match_type));
375 : }
376 :
377 : // Sort and uniquify the vector of PrefixMatch elements.
378 0 : sort(match_list_.begin(), match_list_.end());
379 : typename PrefixMatchList::iterator it =
380 0 : unique(match_list_.begin(), match_list_.end());
381 0 : match_list_.erase(it, match_list_.end());
382 0 : }
383 :
384 : template <typename T>
385 0 : MatchPrefix<T>::~MatchPrefix() {
386 0 : }
387 :
388 : template <typename T>
389 0 : bool MatchPrefix<T>::Match(const BgpRoute *route, const BgpPath *path,
390 : const BgpAttr *attr) const {
391 0 : const RouteT *in_route = dynamic_cast<const RouteT *>(route);
392 0 : if (in_route == NULL)
393 0 : return false;
394 0 : const PrefixT &prefix = in_route->GetPrefix();
395 0 : BOOST_FOREACH(const PrefixMatch &prefix_match, match_list_) {
396 0 : if (prefix_match.match_type == EXACT) {
397 0 : if (prefix == prefix_match.prefix)
398 0 : return true;
399 0 : } else if (prefix_match.match_type == LONGER) {
400 0 : if (prefix == prefix_match.prefix)
401 0 : continue;
402 0 : if (prefix.IsMoreSpecific(prefix_match.prefix))
403 0 : return true;
404 0 : } else if (prefix_match.match_type == ORLONGER) {
405 0 : if (prefix.IsMoreSpecific(prefix_match.prefix))
406 0 : return true;
407 : }
408 : }
409 0 : return false;
410 : }
411 :
412 : template <typename T>
413 0 : bool MatchPrefix<T>::IsEqual(const RoutingPolicyMatch &prefix) const {
414 0 : const MatchPrefix in_prefix = static_cast<const MatchPrefix&>(prefix);
415 0 : return (in_prefix.match_list_ == match_list_);
416 0 : }
417 :
418 : template <typename T>
419 0 : string MatchPrefix<T>::ToString() const {
420 0 : ostringstream oss;
421 0 : oss << "prefix [";
422 0 : BOOST_FOREACH(const PrefixMatch &prefix_match, match_list_) {
423 0 : oss << " " << prefix_match.prefix.ToString();
424 0 : if (prefix_match.match_type == LONGER) {
425 0 : oss << " longer";
426 0 : } else if (prefix_match.match_type == ORLONGER) {
427 0 : oss << " orlonger";
428 : }
429 0 : oss << ",";
430 : }
431 0 : oss.seekp(-1, oss.cur);
432 0 : oss << " ]";
433 0 : return oss.str();
434 0 : }
435 :
436 : template class MatchPrefix<PrefixMatchInet>;
437 : template class MatchPrefix<PrefixMatchInet6>;
438 :
439 : static const map<string, MatchProtocol::MatchProtocolType> fromString
440 : = boost::assign::map_list_of
441 : ("bgp", MatchProtocol::BGP)
442 : ("xmpp", MatchProtocol::XMPP)
443 : ("static", MatchProtocol::StaticRoute)
444 : ("service-chain", MatchProtocol::ServiceChainRoute)
445 : ("aggregate", MatchProtocol::AggregateRoute)
446 : ("interface", MatchProtocol::Interface)
447 : ("interface-static", MatchProtocol::InterfaceStatic)
448 : ("service-interface", MatchProtocol::ServiceInterface)
449 : ("bgpaas", MatchProtocol::BGPaaS);
450 :
451 : static const map<MatchProtocol::MatchProtocolType, string> toString
452 : = boost::assign::map_list_of
453 : (MatchProtocol::BGP, "bgp")
454 : (MatchProtocol::XMPP, "xmpp")
455 : (MatchProtocol::StaticRoute, "static")
456 : (MatchProtocol::ServiceChainRoute, "service-chain")
457 : (MatchProtocol::AggregateRoute, "aggregate")
458 : (MatchProtocol::Interface, "interface")
459 : (MatchProtocol::InterfaceStatic, "interface-static")
460 : (MatchProtocol::ServiceInterface, "service-interface")
461 : (MatchProtocol::BGPaaS, "bgpaas");
462 :
463 : static const map<MatchProtocol::MatchProtocolType, BgpPath::PathSource>
464 : pathSourceMap = boost::assign::map_list_of
465 : (MatchProtocol::BGP, BgpPath::BGP_XMPP)
466 : (MatchProtocol::XMPP, BgpPath::BGP_XMPP)
467 : (MatchProtocol::StaticRoute, BgpPath::StaticRoute)
468 : (MatchProtocol::ServiceChainRoute, BgpPath::ServiceChain)
469 : (MatchProtocol::AggregateRoute, BgpPath::Aggregate)
470 : (MatchProtocol::Interface, BgpPath::BGP_XMPP)
471 : (MatchProtocol::InterfaceStatic, BgpPath::BGP_XMPP)
472 : (MatchProtocol::ServiceInterface, BgpPath::BGP_XMPP)
473 : (MatchProtocol::BGPaaS, BgpPath::BGP_XMPP);
474 :
475 : static const vector<MatchProtocol::MatchProtocolType>
476 : isSubprotocol = boost::assign::list_of(MatchProtocol::Interface)
477 : (MatchProtocol::InterfaceStatic)
478 : (MatchProtocol::ServiceInterface)
479 : (MatchProtocol::StaticRoute);
480 :
481 0 : static bool IsSubprotocol(MatchProtocol::MatchProtocolType protocol) {
482 0 : return (find(isSubprotocol.begin(), isSubprotocol.end(), protocol) !=
483 0 : isSubprotocol.end());
484 : }
485 :
486 0 : const string MatchProtocolToString(MatchProtocol::MatchProtocolType protocol) {
487 : map<MatchProtocol::MatchProtocolType, string>::const_iterator it =
488 0 : toString.find(protocol);
489 0 : if (it != toString.end()) {
490 0 : return it->second;
491 : }
492 0 : return "unspecified";
493 : }
494 :
495 0 : static MatchProtocol::MatchProtocolType MatchProtocolFromString(
496 : const string &protocol) {
497 : map<string, MatchProtocol::MatchProtocolType>::const_iterator it =
498 0 : fromString.find(protocol);
499 0 : if (it != fromString.end()) {
500 0 : return it->second;
501 : }
502 0 : return MatchProtocol::Unspecified;
503 : }
504 :
505 0 : static BgpPath::PathSource PathSourceFromMatchProtocol(
506 : MatchProtocol::MatchProtocolType src) {
507 : map<MatchProtocol::MatchProtocolType, BgpPath::PathSource>::const_iterator
508 0 : it = pathSourceMap.find(src);
509 0 : if (it != pathSourceMap.end()) {
510 0 : return it->second;
511 : }
512 0 : return BgpPath::None;
513 : }
514 :
515 0 : MatchProtocol::MatchProtocol(const vector<string> &protocols) {
516 0 : BOOST_FOREACH(const string &protocol, protocols) {
517 0 : MatchProtocolType value = MatchProtocolFromString(protocol);
518 : // Ignore invalid protocol values.
519 0 : if (value == Unspecified)
520 0 : continue;
521 0 : to_match_.push_back(value);
522 : }
523 :
524 : // Sort and uniquify the vector match protocols.
525 0 : sort(to_match_.begin(), to_match_.end());
526 : vector<MatchProtocolType>::iterator it =
527 0 : unique(to_match_.begin(), to_match_.end());
528 0 : to_match_.erase(it, to_match_.end());
529 0 : }
530 :
531 0 : MatchProtocol::~MatchProtocol() {
532 0 : }
533 :
534 0 : bool MatchProtocol::Match(const BgpRoute *route, const BgpPath *path,
535 : const BgpAttr *attr) const {
536 0 : BgpPath::PathSource path_src = path->GetSource();
537 0 : bool is_xmpp = path->GetPeer() ? path->GetPeer()->IsXmppPeer() : false;
538 0 : bool bgpaas = path->GetPeer() ? path->GetPeer()->IsRouterTypeBGPaaS() : false;
539 0 : BOOST_FOREACH(MatchProtocolType protocol, protocols()) {
540 0 : if (IsSubprotocol(protocol)) {
541 : // Check only if matching protocol is subprotocol
542 0 : if (attr && !attr->sub_protocol().empty()) {
543 0 : std::string matchps = MatchProtocolToString(protocol);
544 0 : if (matchps.compare(attr->sub_protocol()) == 0)
545 0 : return true;
546 0 : }
547 : } else {
548 : BgpPath::PathSource mapped_src =
549 0 : PathSourceFromMatchProtocol(protocol);
550 0 : if (mapped_src != BgpPath::None) {
551 0 : if (mapped_src == path_src) {
552 0 : if (protocol == XMPP && !is_xmpp)
553 0 : continue;
554 0 : if (protocol == BGP && (bgpaas || is_xmpp))
555 0 : continue;
556 0 : if (protocol == BGPaaS && (!bgpaas || is_xmpp))
557 0 : continue;
558 0 : return true;
559 : }
560 : }
561 : }
562 : }
563 0 : return false;
564 : }
565 :
566 0 : string MatchProtocol::ToString() const {
567 0 : ostringstream oss;
568 0 : oss << "protocol [ ";
569 0 : BOOST_FOREACH(MatchProtocolType protocol, protocols()) {
570 0 : string name = MatchProtocolToString(protocol);
571 0 : oss << name << ",";
572 0 : }
573 0 : oss.seekp(-1, oss.cur);
574 0 : oss << " ]";
575 0 : return oss.str();
576 0 : }
577 :
578 0 : bool MatchProtocol::IsEqual(const RoutingPolicyMatch &protocol) const {
579 : const MatchProtocol in_protocol =
580 0 : static_cast<const MatchProtocol&>(protocol);
581 0 : return (protocols() == in_protocol.protocols());
582 0 : }
|